Results 1 to 5 of 5
- 01-21-2011, 07:43 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
A Question Regarding Pattern Matching
Hello
I want to extract words from a string followed by a - .
for example in string : "sss-ddd-fff-" , i want the following groups :
1- "sss-"
2- "ddd-"
3- "fff-"
I wrote this regex for doing this : "((?:[^-]+)-)+"
But this isn't working as i said .
Could you explain what regex should i write for the mentioned task ? and what is the meaning of my
regex ?
thanks
- 01-21-2011, 09:11 AM #2
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
in this simplest sense , you can just use this pattern
Java Code:Pattern.compile("\\w+-");
- 01-21-2011, 09:26 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
That pattern doesn't capture any groups , i want groups like i said .
- 01-21-2011, 02:07 PM #4
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
outputJava Code:public class StringDemo{ private static Pattern ex = Pattern.compile("\\w+-"); private static Matcher match; public static void main(String[] args){ String str = "sss-ddd-fff-"; match = ex.matcher(str); while ( match.find() ){ System.out.println( match.group()) ; } } }
Java Code:$ java StringDemo sss- ddd- fff-
- 01-21-2011, 03:38 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
pattern matching with two arrays
By jessie in forum New To JavaReplies: 10Last Post: 11-24-2010, 04:00 PM -
myPatterns: pattern matching in custom notations for Java & JavaScript
By Nic Volanschi in forum Java SoftwareReplies: 0Last Post: 06-29-2010, 08:16 PM -
String matching a pattern
By vividcooper in forum New To JavaReplies: 7Last Post: 01-07-2010, 10:30 PM -
Will someone pliz help me with this code on pattern matching
By nmvictor in forum New To JavaReplies: 1Last Post: 10-27-2009, 07:33 PM


LinkBack URL
About LinkBacks

Bookmarks