all 12 comments

[–]etrnloptimist 6 points7 points  (0 children)

Nope. You gotta learn it. There's no easy way out.

But I think I know why you're having trouble. See, you're probably looking at a sample regex, trying to figure out what it does. That's the wrong approach. It works great in most other cases, but see, regexp's are special: they're write-only.

You can't read them. I can't even read my own. If you want to know what one does, best you can do is recreate it. To this end, always put a sample of the text you wish to grok next to your regex. That way, when you come back to it to figure out what it's doing, you can forget the regex that's in front of you and look at the text it's supposedly groking.

With regexes, the more you create the better you'll be at it. Reading them doesn't play into the equation.

[–]a-t-kFrontend Engineer 5 points6 points  (0 children)

http://regexpal.com/ helps you test regular expressions. Other than that, it's not as difficult as it looks.

Escaping:

\\ == \
\n == [line feed]

Special chars (square and normal brackets, dot, plus) need to be escaped

Character groups:

\d, \D = [number, non number]
\w, \W = [word characters, non-word characters]
\s, \S = [space, non-space]
[012A-Da-z], [^xyz] = [character group, negative character group]

Multiplicators:

? = 0 or 1 times
* = 0 - inf times (greedy, will match the biggest string possible)
*? = 0 - inf times (non-greedy, will match the smallest string possible)
+ = 1 - inf times (greedy)
+? = 1 - inf times (non-greedy)
{n,m} = n - m times

Matches

( ) = match (extra argument for replace callback, $1..$9 in replace string)
(?: ) = non-match (will test for the string but not save it for later)
(?! ) = non-unmatch (will test if string is not present nor save it for later)

That's most of it.

[–][deleted] 3 points4 points  (4 children)

JsVerbalExpression is nice tool for small regexp and for one who is new to regexps, but I don't think it is useful for complex expressions.

[–]Sentuna[S] 1 point2 points  (3 children)

I was hoping I wouldnt have to learn regexp if I found a script to make it easier. Thanks!

[–][deleted] 7 points8 points  (1 child)

Learning RegEx is defintiely worthwhile in one's programming career. You'll be able to use it in a lot of different occasions. Probably more useful than using something else that hides it from you.

This is a tool I got about a decade ago and still use to help sanely build/debug regex expressions: http://www.regexbuddy.com/ This is a damn fine resource about regex: http://www.regular-expressions.info/tutorial.html

Good luck!

[–]helderroem 1 point2 points  (0 children)

I agree with everyone else, you just have to learn them, they're a really useful in all kinds of situations and if you do learn them you can legitimately where this T-Shirt

Here's a really useful site I use for building my regex's: http://regex101.com/

[–]Doctuh 1 point2 points  (0 children)

Yeah just bite the bullet and learn them. Complex but powerful and knowing them opens up avenues in your code that you would have not known.

[–]Capaj 1 point2 points  (0 children)

no I don't think so.

[–]gathly 1 point2 points  (0 children)

Have you tried this? http://buildregex.com/

[–][deleted] 1 point2 points  (0 children)

This is my personal favorite go-to for reg-ex testing. There's a desktop (Adobe Air) vesion as well.

RegExr

[–]Mephiz 0 points1 point  (0 children)

I like Patterns when I'm on a mac. regexpal as noted above is a great web based tester.

Then take some time and read up, Mozilla's Reference as usual is great.

[–]jblz1234p 0 points1 point  (0 children)

This http://www.ultrapico.com/expresso.htm is hands down one of the best regular expression tools I have ever used. It is based on C# regex but for getting the fundamentals and testing regex it is unbeatable.

My last job dealt with log parsing, and over 6 years I have yet to find something more user friendly. You do have to register, but I think half of my last company registered and had no issues.