Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Regular Expressions Question

Regular Expressions Question
Thread Tools
Ghoser777
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Sep 23, 2006, 09:50 PM
 
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!
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 23, 2006, 10:59 PM
 
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'."
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Sep 24, 2006, 02:00 AM
 
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];
     
wadesworld
Grizzled Veteran
Join Date: Apr 2001
Status: Offline
Reply With Quote
Oct 8, 2006, 01:35 AM
 
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.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Oct 8, 2006, 04:06 AM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 01:00 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,