Results 1 to 2 of 2
Thread: Basic regular expression help
- 06-25-2009, 10:20 PM #1
Member
- Join Date
- Jun 2009
- Posts
- 1
- Rep Power
- 0
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.
When I loop through the found groups i have two,Java Code:String patternStr = "(<lvs>.+?</lvs>)+?"; // Compile and use regular expression Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(text); matcher.matches();
<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?
- 06-26-2009, 01:29 AM #2
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
-
Quantifiers in Regular Expression
By cdpm in forum java.utilReplies: 0Last Post: 12-24-2008, 01:03 PM -
Complex Regular Expression HELP
By hiklior in forum New To JavaReplies: 1Last Post: 04-30-2008, 01:52 PM -
Regular Expression Challange
By hiklior in forum New To JavaReplies: 2Last Post: 04-24-2008, 05:05 AM -
regular expression for unicode
By tharhan in forum Advanced JavaReplies: 0Last Post: 04-01-2008, 10:53 PM -
Regular expression with Intersections
By Java Tip in forum Java TipReplies: 0Last Post: 01-09-2008, 12:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks