|
-
January 18th, 2011, 09:35 AM
#1
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
-
January 18th, 2011, 01:40 PM
#2
Re: A Question Regarding Pattern Matching
Not the best with regular expressions but I'll take a stab here.
I would seem that you want 3 groups of 3 character length strings and the following "-" of each. If that is the case then maybe [a-z][a-z]][a-z]- three times in a group.
[([a-z][a-z][a-z]-)([a-z][a-z][a-z]-)([a-z][a-z][a-z]-)]
Your expression:
((?:[^-]+)-)+
Seems to be
?:[^-]+ as a non-capturing group that doesn't capture [^-] or + in the first group then an "-" in the next group then a "+"
Are you trying to capture strings of arbitrary length and the following "-" of each?
Maybe if you were to dissect your expression and explain what you think each piece is supposed to do then it would give a better idea of what you are trying to accomplish and you might stumble on the answer in the process. Often I start a post and when I am just about to finalize it I read my own explanation and find my own error. =D
Also how is your expression currently working? Is it capturing anything? If so, what is it capturing? If it isn't working at all then are you sure you have it implemented correctly (i.e. a proper Pattern class with a corresponding Matcher class)?
Anyway good luck.
-
January 18th, 2011, 02:35 PM
#3
Re: A Question Regarding Pattern Matching
While your solution works for his single given example, it will not work for anything other than 3 - 3 - 3.
How and where are you using regular expressions? If you are indeed using Java, then doing a simple:
myString.split("-");
Will give you what you need.
As far as regex goes, I think that "[./-]+" will basically get the dash locations. You can formulate what you need it to do from there.
BTW, the "+" means one or more times, not that the expression is looking for the + symbol.
-
January 19th, 2011, 01:42 AM
#4
Re: A Question Regarding Pattern Matching
to djgarrison :
yes i want them to be of arbitrary length .
and yes i think i implemented it correctly other regular expressions give me my expected results .
to ProgramThis :
actually i'm writing a simple DBMS and i want to check syntax of statements and then extract
information from those statements . I can do it with split and other methods but this is just a part of
a statement and doing this with pattern matching would be a LOT easier .
I never saw an example that use group capturing with a quantifier like + . can you give me a working
example ? is it even possible ?
-
January 19th, 2011, 04:28 AM
#5
Re: A Question Regarding Pattern Matching
 Originally Posted by hadi123
I can do it with split and other methods but this is just a part of
a statement and doing this with pattern matching would be a LOT easier .
If you only have that simple pattern (series of characters of arbitrary length separated by a -) the solution proposed by ProgramThis is perfect. Using the split method you are actually using a regular expression and you will get all the matching groups in an array. I don't know why you believe it would be a lot easier with regular expressions, when you have to do what split does for you. I do use regular expressions quite often, so I know what I'm talking about.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|