Results 1 to 6 of 6

Thread: Regex string with quotes

  1. #1
    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

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    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);

  6. #6
    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;		
    	}

Similar Threads

  1. Problem in retrieving string with Regex
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 04:53 AM
  2. Regex - compose a string
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 12:38 AM
  3. Problem with Regex in returning string
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 02:37 AM
  4. Regex replace an integer with string
    By New ID in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 12:35 AM
  5. Regex strip out all non alpha Characters from a String
    By Chhaya in forum Software Development
    Replies: 2
    Last Post: 22-05-2009, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •