Regular Expression help...!

Status
Not open for further replies.

fitten

Ars Legatus Legionis
55,634
Subscriptor++
Your problem is two-fold. The first is that parenthesis are a part of the regex language so you have to escape them for an exact match on those characters. The second bit is fairly easy... dot/period "." is "any character" and if you want to match on the condition that there are one or more characters, you put a + after it. If you want to match on the condition that there are zero or more characters, you put an * after it.

So... something like this: \(.+\) means match a ( followed by one or more characters and then a ). It will not match "()" though. If you need to match "()" then you want something like \(.*\). If you want to actually 'remember' those characters then you use parenthesis around them (that's the capture) so something like \((.+)\) should capture "an12345" in the above. However, you'll need to also match the space between "Joe Bloggs" and the stuff in your parens to remove it properly (unless you do a trim on it later).
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
Just make sure if you have following use case with multiple blocks possible in your data model: "Foo (first) Bar (second) Recognition"
Regular expression matching is greedy by default (if I remember correctly) so the previously posted expressions will match the entire "(first) Bar (Recognition)".
So after trimming you will be left with "Foo Recognition" instead of "Foo Bar Recognition".
 
Alamout":28phppbz said:
^ If it is a problem, there is a solution. You can make the expression non-greedy by adding a ?, as in: \(.*?\) (note: this might be different in the language you're using)

So, in the example above, it would match "(first)" and "(second)" separately.

I would highly recommend using lazy quantifiers, *? and +? (or the equivalent), as much as possible. They will help you avoid common problems, and in most situations, dramatically speed up your searches.
 
This thread is proof positive that absolutely no one understands or implements regexps right. Here's as close as I can get to intent:
Code:
\([^)]*\)

In plain understandable unambiguous English:
Code:
Starts with '('
Contains in the middle 'anything but ), including nothing'
Ends with ')'

For testing, I always try to use an interactive tool like this( http://www.regular-expressions.info/jav ... ample.html ).
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
what are you wanting to do with:
Code:
Joe Bloggs (aa(bb)cc)
Joe Bloggs (aa(bb)(cc)dd)
Joe Bloggs (aa(bb))((cc)dd)
further fun left as exercise for reader.
if you want to bracket match you *can no longer use regular expressions*

Since doing so in code is trivial (okay more painful in something like c, but still not that hard), just do it in code.
 
bravo on the ungreedy. I didn't think of it either.
Code:
Joe Bloggs (aa(bb)(cc)dd)
I built a regex for infinitely deep html which looks like it would do for the scenario requested here. This was a while back, so go easy on the code.
Code:
(<([^>\s]+)[^>]*>)([^\2]+)(<\/\2>)
done for () it would be
Code:
(\((?:[^()]*(\([^()]*\))*[^()]*)\))
This only goes one layer deep, but if it gets deeper I suspect you have a bigger problem
 
Ardax":defr26fg said:
This only goes one layer deep, but if it gets deeper I suspect you have a bigger problem
Yeah, like you're trying to use regular expressions where they're clearly not suited for the job at hand.

Or your brain is broken because it's attempting to make sense of line noise.
That's not to say the expression couldn't be extended to go deeper, I was going to stack it up to go up to 10 layers deep, but it didn't seem relevant to the problem. Besides, why no love for regex? I have seen regex used to blow up more than one "well-programmed" setup before. They're great ways to get around restrictions like
Code:
if (input.IndexOf("while") > -1){return; //bad user!}
Lesson 1: if there's code out there that's secure, don't invent your own security.
 

fitten

Ars Legatus Legionis
55,634
Subscriptor++
Jackass JoeJoe":2on803cj said:
This thread is proof positive that absolutely no one understands or implements regexps right. Here's as close as I can get to intent:
Code:
\([^)]*\)

In plain understandable unambiguous English:
Code:
Starts with '('
Contains in the middle 'anything but ), including nothing'
Ends with ')'

For testing, I always try to use an interactive tool like this( http://www.regular-expressions.info/jav ... ample.html ).

Here's an online tool for constructing regex: http://txt2re.com/ Very nice... put your string in, show matches, and it gives you a diagram asking which ones you want to extract and out pops your regex.

Also, I've used RegexBuddy a bunch in the past (app, various platforms).
 
Ardax":208wipt2 said:
Enlighten me: How is that different than an ungreedy .*?

Readability and maintainability for regexp is still a nightmare. My version won't fluctuate in meaning as the regexp grows in complexity. It's always best to be explicit as possible for the two reasons listed earlier. Also, explicit terms are the best rule of thumb for performance [keeping in mind that performance should always be thought of lastmost].

Otherwise, As-Is, there would only be minor differences in the compiled tuples/graphs but mine is likely to perform slightly better and be much less complex. IIRC, my version should produce 3 (or 2 optimized) tuples and requires no backreferencing overhead [since no grouping was used].

pseudo example tuples
Code:
number / operation / true goto / false goto
0: match '(' 1 0
1: match ')' 2 1
2: end
 
fitten":1xswszz3 said:
Jackass JoeJoe":1xswszz3 said:
This thread is proof positive that absolutely no one understands or implements regexps right. Here's as close as I can get to intent:
Code:
\([^)]*\)

In plain understandable unambiguous English:
Code:
Starts with '('
Contains in the middle 'anything but ), including nothing'
Ends with ')'

For testing, I always try to use an interactive tool like this( http://www.regular-expressions.info/jav ... ample.html ).

Here's an online tool for constructing regex: http://txt2re.com/ Very nice... put your string in, show matches, and it gives you a diagram asking which ones you want to extract and out pops your regex.

Also, I've used RegexBuddy a bunch in the past (app, various platforms).
Wow. Just pick up a CS book and learn the theory!
 

fitten

Ars Legatus Legionis
55,634
Subscriptor++
Kalila":1sdw7k7q said:
Wow. Just pick up a CS book and learn the theory!

Already did... ages ago. Then I learned that it's easy to use tools that do such things for you and you can test what you're doing easily. That link to the online generator is one a coworker found a couple weeks ago and it looked neat. I haven't really used it yet. RegexBuddy is nice to test with, though, but you have to generate the regex on your own for the most part.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
Wow. Just pick up a CS book and learn the theory!
It is the implementation that screws you. To wit:
Code:
s/(pattern1.*)$/\1 ADDITIONAL TEXT/g
That's a valid regex, right? Okay, try using it as your search and replace in vi. You need to escape the parens, as it tries to match actual parens. Works fine with perl-style regex, though.
Code:
pattern1 is bullshit.
~
~
:%s/(pattern1.*)$/\1 ADDITIONAL TEXT/g
E486: Pattern not found: (pattern1.*)$
:%s/\(pattern1.*\)$/\1 ADDITIONAL TEXT/g
pattern1 is bullshit. ADDITIONAL TEXT
~
~
Regex's have to be one of the least portable constructs.
 
Kalila":pfq5e7ob said:
fitten":pfq5e7ob said:
Jackass JoeJoe":pfq5e7ob said:
This thread is proof positive that absolutely no one understands or implements regexps right. Here's as close as I can get to intent:
Code:
\([^)]*\)

In plain understandable unambiguous English:
Code:
Starts with '('
Contains in the middle 'anything but ), including nothing'
Ends with ')'

For testing, I always try to use an interactive tool like this( http://www.regular-expressions.info/jav ... ample.html ).

Here's an online tool for constructing regex: http://txt2re.com/ Very nice... put your string in, show matches, and it gives you a diagram asking which ones you want to extract and out pops your regex.

Also, I've used RegexBuddy a bunch in the past (app, various platforms).
Wow. Just pick up a CS book and learn the theory!


I use rubular.com
 
Mister E. Meat":2bzapkiu said:
fitten":2bzapkiu said:
Your problem is two-fold.
This was the perfect setup for this line. I'm surprised no one jumped on it.
Jamie Zawinski":2bzapkiu said:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
;)
why no love for regex? It's a tool. Any tool can be used improperly, but that doesn't make it a bad tool.
 

Mister E. Meat

Ars Tribunus Angusticlavius
7,571
Subscriptor
Suicidolt":1kl45t0v said:
why no love for regex? It's a tool. Any tool can be used improperly, but that doesn't make it a bad tool.
I actually love regex and I use them all the time when I need to. The problems I find with them is that to get them (near) bulletproof, you end up with a-whole-nother program running inside your program, in a completely different syntax and to compound that, you can't easily debug it.

I mean, just look at this discussion about matching an email address. So yeah, the quick and dirty
Code:
\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
is great for most email addresses, but as he mentions, it's not a guarantee.

By the time you've gone through all the permutations, you end up with something like
Code:
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|
\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|
\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:
(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
which still isn't right - again as he mentions - since it will match email addresses that have non-existent top level domains, like .fubar, and good luck maintaining that long-term.

Edit: broke h-scroll, so reformatted the longer one - it might not work anymore when put in your favorite regex.
 
Mister E. Meat":rm1ilzu7 said:
Suicidolt":rm1ilzu7 said:
why no love for regex? It's a tool. Any tool can be used improperly, but that doesn't make it a bad tool.
I actually love regex and I use them all the time when I need to. The problems I find with them is that to get them (near) bulletproof, you end up with a-whole-nother program running inside your program, in a completely different syntax and to compound that, you can't easily debug it.

I mean, just look at this discussion about matching an email address. So yeah, the quick and dirty
Code:
\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
is great for most email addresses, but as he mentions, it's not a guarantee.

By the time you've gone through all the permutations, you end up with something like
Code:
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|
\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|
\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:
(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
which still isn't right - again as he mentions - since it will match email addresses that have non-existent top level domains, like .fubar, and good luck maintaining that long-term.

Edit: broke h-scroll, so reformatted the longer one - it might not work anymore when put in your favorite regex.

That assumes that regex is the best thing you should use to search for an email address. TBH, you couldn't ever confirm that it's an email address for sure without sending an email to it. Use the right tool for the right job is the phrase that comes to mind. In fact, one of my favorite uses for Regex is to shorten 15-20 if-else's (or if || || || ...) to one simple if.
 
Ardax":2g8jh4um said:
Email addresses and regexes are bad juju.

You could end up with beasts like these, or worse, this 6 KB monster.

As far as I'm concerned, I'll just check for an @ somewhere in the middle and call it good. Beyond that, just submit the message to your mail server and see what it does.

Oh wow

Code:
(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:
\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(
?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ 
\t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\0
31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\
](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+
(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:
(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z
|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)
?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\
r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[
 \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)
?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t]
)*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[
 \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*
)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)
*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+
|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r
\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:
\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t
]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031
]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](
?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?
:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?
:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?
:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?
[ \t]))*"(?:(?:\r\n)?[ \t])*)*:(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] 
\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|
\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>
@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"
(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t]
)*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\
".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?
:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[
\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-
\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(
?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;
:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([
^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"
.\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\
]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\
[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\
r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] 
\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]
|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \0
00-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\
.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,
;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?
:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*
(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".
\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[
^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]
]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*(
?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\
".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(
?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[
\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t
])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t
])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?
:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|
\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:
[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\
]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)
?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["
()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)
?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>
@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[
 \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,
;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t]
)*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\
".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?
(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".
\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:
\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[
"()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])
*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])
+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\
.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z
|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(
?:\r\n)?[ \t])*))*)?;\s*)

That's insane.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
That assumes that regex is the best thing you should use to search for an email address.
Which goes back to the beginning - someone looks at a problem, thinks regex is the answer, and now they have two problems :) If you have never looked at how to process email addresses, regex *looks* to be the right answer. Three days, 72 advil, and a case of your favorite caffeine product, you finally realize it is really the wrong answer. That's a hell of a cost.
 
ronelson":l2ck0gzx said:
That assumes that regex is the best thing you should use to search for an email address.
Which goes back to the beginning - someone looks at a problem, thinks regex is the answer, and now they have two problems :) If you have never looked at how to process email addresses, regex *looks* to be the right answer. Three days, 72 advil, and a case of your favorite caffeine product, you finally realize it is really the wrong answer. That's a hell of a cost.

I think that's a bit misleading. Regexes aren't the root problem, a lack of knowledge about regexes, and their limitations, is. They're just the first tool that looks powerful enough to do the job in an easy way if you don't know better. I don't think most of the people who try to use regexes and fail look at the problem and say "well, I don't think email addresses are a regular language, but I bet they're powerful enough with the add-ons that the language I'm using provides, so let's go for it instead of trying to write a more complicated parser." I think they look at it and say "hmm, maybe this will save me a lot of time" but they don't even know what really doing that validation in a correct way would entail -- so that time spent failing to do it with a regex is just time that would've been spent creating some crazy approximation of a parser that isn't going to work any better anyway.
 
ronelson":s2iefet8 said:
I think that's a bit misleading.
It's a funny quote from a well-known hacker, nothing more. Stop being so pedantic :)

It is funny, but it's also annoying when, say, your boss sees it and then gets super inquisitive about "do we really want to be using regular expressions?" when you use a simple 8-character regex instead of an 8 line loop or something. So it's kinda a pet peeve. :devious:
 
Status
Not open for further replies.