Jump to content

[SOLVED] regex character classes


soycharliente

Recommended Posts

From looking at a tutorial ...

 

A dot (.) means any character so long as it's not a newline character.

[] are used to define character classes to apply rules to or for allowing.

 

So, what does this mean?

 

([-+.])*

 

Does that mean:

Zero or more of

A. either of those 3 literal characters

B. either a - or + or any other character so long as it's not a newline character

 

Common sense would tell me A is right, but I just want to be sure.

Link to comment
Share on other sites

well, first, a '-' is also a special character, so you would need to escape it with a '\'

 

but '-' and '+' are included with '.', so that would be redundant. you should never need a . inside []

A dash ('-') doesn't need to be escaped ('\') inside a character class when it's at the beginning or end.

 

Also, discomatt is correct.

 

It's well worth buying this regex software:

http://www.regexbuddy.com

 

and this book - Mastering regular expressions:

http://www.oreilly.com/catalog/regex3/

 

 

Link to comment
Share on other sites

So, what does this mean? ([-+.])*

 

The others have already explained this, but what is the context of this pattern? ([-+.]*) would likely be better, but, again, this depends on the context.

 

I can't find anything that says the comma is a special character.

So what does this mean?

([,]|[,])\s*

 

It's not--that's a confusing pattern. It should be ,\s* unless you really need to capture the comma.

Link to comment
Share on other sites

Grab the regexbuddy free trial.. it's a great piece of software. You will become addicted to it though, so be ready to shell out a couple bucks to purchase ;) It explains every detail of your expression in plain english

 

http://www.softpedia.com/get/Programming/Other-Programming-Files/RegexBuddy.shtml

 

Your regex does the following:

 

match either a comma or a comma exactly once, capture it in group 1

match a whitepace character (space, tab, linebreak, ect) 0 or more times, as many times as possible, giving back as needed

Link to comment
Share on other sites

... but what is the context of this pattern?

 

Someone gave me this email regex a while ago.

 

/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(([,]|[,])\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*$/

 

It works better than any other pattern I've ever seen. I've tried others in regex testers online and can make each of them fail with a simple address.

 

I've been using it for a while and never bothered to try and figure out the pattern. Just sat down today and tried to work through it, but as you can see, getting confused because of things like "looking for a comma or a comma".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.