Results 1 to 2 of 2
  1. #1
    predhme is offline Member
    Join Date
    Jun 2009
    Posts
    1
    Rep Power
    0

    Default Basic regular expression help

    I am really new to regular expressions and am attempting to achieve the following.

    From a string, find all groups that begin and end with <lvs> and </lvs>.

    The string may contain multiple <lvs></lvs> groups. We don't care about anything inbetween.

    For example: <lvs>first</lvs><lvs>second</lvs><lvs>third</lvs>

    I have the following code.

    Java Code:
    String patternStr = "(<lvs>.+?</lvs>)+?";
    
    // Compile and use regular expression
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(text);
    matcher.matches();
    When I loop through the found groups i have two,
    <lvs>first</lvs><lvs>second</lvs><lvs>third></lvs>
    <lvs>third</lvs>

    I am expecting
    <lvs>first</lvs><lvs>second</lvs><lvs>third></lvs>
    <lvs>first</lvs>
    <lvs>second</lvs>
    <lvs>third></lvs>

    I understand that group at position 0 is the entire matched string, but for whatever reason the first and second groups aren't being found. I have been looking at the API docs and googling for the last couple hours. Could someone lend a hand?

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    are you sure you are using matcher correctly?
    Java Code:
    [COLOR=NAVY]import[/COLOR] java[COLOR=BLACK].[/COLOR]util[COLOR=BLACK].[/COLOR]regex[COLOR=BLACK].[/COLOR][COLOR=BLACK]*[/COLOR][COLOR=BLACK];[/COLOR]
    [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]class[/COLOR] Test [COLOR=BLACK]{[/COLOR]
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]void[/COLOR] main[COLOR=BLACK]([/COLOR]String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] args[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        String text [COLOR=BLACK]=[/COLOR] [COLOR=SILVER]"<lvs>first</lvs><lvs>second</lvs><lvs>third</lvs>"[/COLOR][COLOR=BLACK];[/COLOR]
        String patternStr [COLOR=BLACK]=[/COLOR] [COLOR=SILVER]"(<lvs>.+?</lvs>)+?"[/COLOR][COLOR=BLACK];[/COLOR]
    
        [COLOR=GREEN][I]// Compile and use regular expression[/I][/COLOR]
        Pattern pattern [COLOR=BLACK]=[/COLOR] Pattern[COLOR=BLACK].[/COLOR]compile[COLOR=BLACK]([/COLOR]patternStr[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        Matcher matcher [COLOR=BLACK]=[/COLOR] pattern[COLOR=BLACK].[/COLOR]matcher[COLOR=BLACK]([/COLOR]text[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        
        [COLOR=PURPLE]int[/COLOR] i[COLOR=BLACK]=[/COLOR][COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=NAVY]if[/COLOR][COLOR=BLACK]([/COLOR]matcher[COLOR=BLACK].[/COLOR]matches[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
          System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR]i [COLOR=BLACK]+[/COLOR][COLOR=SILVER]"= "[/COLOR][COLOR=BLACK]+[/COLOR] matcher[COLOR=BLACK].[/COLOR]group[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR]
        
        matcher[COLOR=BLACK].[/COLOR]reset[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        
        [COLOR=NAVY]while[/COLOR][COLOR=BLACK]([/COLOR]matcher[COLOR=BLACK].[/COLOR]find[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
          System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR][COLOR=BLACK]++[/COLOR]i [COLOR=BLACK]+[/COLOR][COLOR=SILVER]"= "[/COLOR][COLOR=BLACK]+[/COLOR] matcher[COLOR=BLACK].[/COLOR]group[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR]
      [COLOR=BLACK]}[/COLOR]
    [COLOR=BLACK]}[/COLOR]
    
    [COLOR=GREEN]/*
    S:\>javac Test.java
    S:\>java Test
    0= <lvs>first</lvs><lvs>second</lvs><lvs>third</lvs>
    1= <lvs>first</lvs>
    2= <lvs>second</lvs>
    3= <lvs>third</lvs>
    
    */[/COLOR]
    Last edited by angryboy; 06-26-2009 at 01:41 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Quantifiers in Regular Expression
    By cdpm in forum java.util
    Replies: 0
    Last Post: 12-24-2008, 01:03 PM
  2. Complex Regular Expression HELP
    By hiklior in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 01:52 PM
  3. Regular Expression Challange
    By hiklior in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 05:05 AM
  4. regular expression for unicode
    By tharhan in forum Advanced Java
    Replies: 0
    Last Post: 04-01-2008, 10:53 PM
  5. Regular expression with Intersections
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 12:03 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
  •