Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Regex string with quotes

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 07-02-2010
Member
 
Join Date: Dec 2009
Posts: 296
Regex string with quotes
  

Hello,
I do not have absolute control over the regular expression and I regexp to split a string of such
Code:
"string" 2 "another string"
in a 1st time I told myself that I would get the channels between quote (my ultimate goal is to retrieve each element, or in Example 3 channels)
after reading the doc and I tried a few examples:
Code:
Pattern pattern = Pattern.compiles("\"(.*)\"");
My aim is to bring the regex string with quotes. But this expression does bring me back anything at all. Thank you in advance for your help

Reply With Quote
  #2  
Old 07-02-2010
Member
 
Join Date: Nov 2009
Posts: 446
Re: Regex string with quotes

Hello,
The regex does may not necessarily be relevant in this case, may be a simple split may be more effective. Just have a look at the following code.
Code:
String s = "\"chain\" 2 \"another sing\"";
    String[] chspt = sing.split("\"");
    for(String subsing: chspt){
        if (! subsing.equals(""))
        System.out.System.out.println(subsing);
    }
You can try this code and if you have any problems then you can post and please post with the error you get.
Reply With Quote
  #3  
Old 07-02-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: Regex string with quotes

Hello,
The above code is correct and you should try that, alternatively with the regex, you can do that, but I am not convinced that is a super as a solution:
Code:
{
    Pattern ptn = Pattern.compiles("([^\"]*)");
    Matcher mth = ptn.mth("\"chain\" 2 \"another string\"");
    while (mth.find()) {
        if (! mth.group().equals(""))
        System.out.System.out.println(mth.group());
    }
You can try both the code this one and the above post and see which one is suitable for your program.
Reply With Quote
  #4  
Old 07-02-2010
Member
 
Join Date: Dec 2009
Posts: 296
Re: Regex string with quotes

Hello,
Thank you very much for your reply. Little precision that it is the method split (on string) also uses pattern so it is not slower and you can then use the expression Unfortunately, your solutions do not work because if you take the string following the result is incorrect
Code:
"string" 1 2 "another string"
If you have any other solution to suggest it will be great. Thanks in advance.
Reply With Quote
  #5  
Old 07-02-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: Regex string with quotes

Hello,
I have a code with me, though I am not sure it will work with your program that is I have no idea about your exact needs. Just go through it and if you feel it will work with you, ten you can use it in your program.
Code:
 int itr = 0;
    while (i <chaineSplit.size()) {
        chain[itr] = "";
        if (chaineSplit.get(i).startsWith("\"")) {
        while (! chaineSplit.get(i).endsWith("\"")) {
            chain[itr] + = chsp.get(i) + " ";
            i + +;
        }
        }
        chain[itr] + = chsp.get(i);
        i + +;
        itr + +;
    }
    for (String s: string)
        if (s! = null)
        System.out.System.out.println(s);
Reply With Quote
  #6  
Old 07-02-2010
Member
 
Join Date: Nov 2009
Posts: 356
Re: Regex string with quotes

Hello,
Here is the code, you can have a look at it
Code:
	Public static List <String> getWords(String input) {
		
		ArrayList rst = <String> new ArrayList <String>();


Pattern pat = Pattern.compiles("((?<!\\\\)\".*?(?<!\\\\)\")|([^\"\\s] +) ");
Matcher mth = pat.mth(input);
		
		while (mth.find()) {
			rst.add(mth.group());
		}
		
		rst.TrimToSize();
		return rst;		
	}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Regex string with quotes"
Thread Thread Starter Forum Replies Last Post
Problem in retrieving string with Regex Aaliya Seth Software Development 5 11-02-2010 05:53 AM
Regex - compose a string Aaliya Seth Software Development 5 09-02-2010 01:38 AM
Problem with Regex in returning string Ash maker Software Development 5 05-02-2010 03:37 AM
Regex replace an integer with string New ID Software Development 5 05-02-2010 01:35 AM
Regex strip out all non alpha Characters from a String Chhaya Software Development 2 22-05-2009 07:51 PM


All times are GMT +5.5. The time now is 03:06 AM.