|
|
Regular Expressions Question
|
|
|
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
My professor was so kind as to give us a question about regular expressions for homework without ever mentioning it in class. I've never understood fully how they worked, and I'd like to run by the expression by everyone and see if you can tell me what it means:
"\\s+|[.]*"
My interpretation:
\\s - space character
\\s+ - 1 or more space characters
[.] - match one of the set (only .)
[.]* - match 0 or more consecutive .'s
\\s+[.]*
We have a match when we have 1 or more space characters, or we have 0 or more dots.
If that's right, then I don't understand what this means:
"\\s*|[.]*"
Changing the plus to the star makes it so that in the String, none of the substring's containing a dot are matched... which I find odd. I do notice that it seems symmetric:
"[.]+|\\s*" works as well as the original, but
"[.]*|\\s*" captures the .'s, but not the spaces....
Help!
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
As I understand it (I'm not well studied on the theory of regular expressions, but I've used them enough to be familiar), the regex is evaluated left-to-right. If you have /\s*|[.]/, everything is going to fall under the blanket of /\s*/ — even if there are zero spaces to capture, zero spaces is included in that — so the right side will never be matched against. You'll find that /[.]*|\s+/ also doesn't capture any spaces, even though it contains the same symbols in reverse order.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
Pretty much what Chuckit said. That regexp seems a little silly to me.
\s+[.]* would make sense, but with the pipe in there, it doesn't…
|
[Wevah setPostCount:[Wevah postCount] + 1];
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Apr 2001
Status:
Offline
|
|
I agree that's a silly regex. Assuming you haven't made a typo, I read it as:
Match:
\s one or more times - (that's a literal backslash s - why? Because the first part was \\ which escapes the backslash and takes the special meaning away from \s)
or match:
a literal dot 0 or more times.
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
In some languages, regexps have to be specified as a string, and the backslash in the string needs to be escaped or else the regex engine will not receive any backslashes itself. I assumed he was using a language like that.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|