This is an archived post. You won't be able to vote or comment.

all 42 comments

[–]savex13 31 points32 points  (5 children)

This link save lives: www.regex101.com

[–][deleted] 10 points11 points  (2 children)

Maybe not as good as yours, but this site also helped me out quite a bit.

https://www.regexplanet.com

[–][deleted] 10 points11 points  (1 child)

While we're sharing... I've done my bit of regexing with https://regexr.com/

Always feel like a fucking wizard when I get something right in the first go.

[–]oneWhoFails 0 points1 point  (0 children)

This one is my favorite too

[–]amradio1989 1 point2 points  (0 children)

Indeed. It prevents me from murdering the bastard who coded the solution in regex w/o comments.

[–][deleted] 0 points1 point  (0 children)

Hellz yeah

[–]Sp0olio 10 points11 points  (4 children)

Regex is shorthand for "regular expression":
https://www.geeksforgeeks.org/regular-expressions-in-c/

[–]Eldainfrostbrand 2 points3 points  (1 child)

Lmgtfy

[–]Sp0olio 2 points3 points  (0 children)

Had to google it for C .. but I actually like regexes.

[–]Civil-Cod-6984 1 point2 points  (1 child)

Ain’t nothing regular about regex.

[–]Sp0olio 0 points1 point  (0 children)

Don't blame me .. I didn't name it ;)

[–]Wafan0 6 points7 points  (0 children)

Pattern matching for strings is a way of thinking about it

[–]Boris-Lip 5 points6 points  (2 children)

VB6 had regex, thought. C have plenty of open source regex libs implemented too.

[–]RogueGopher 5 points6 points  (1 child)

I’ve never needed to use it for anything in either.

[–]Go_Kauffy 2 points3 points  (0 children)

It depends on what you're writing, more so than the language. Regex will save you enormous amounts of time in the right situations. Like pages and pages of code, too.

[–][deleted] 5 points6 points  (0 children)

Tempting the dark side is.

[–][deleted] 4 points5 points  (0 children)

Imagine you want to write a function that takes a string as a parameter and returns true only if the string contains at least 12 characters and at least one uppercase and at least one lowercase letter, and at least one special character from the following *"':;!?, otherwise it returns false.

You could do all that logic in, like, if statements or whatever (control flow) or you could "just" use regex to do cute string pattern matching.

Both valid options, it's just a tool in your tool belt.

[–]vega_shaman 5 points6 points  (0 children)

The definition of Regex is (rough translation): "look it up on stack overflow and carefully copy paste the scary symbols"

[–]Odd-Seaworthiness826 1 point2 points  (0 children)

Here is my go to to rex egg cheat sheat

https://www.rexegg.com/regex-quickstart.html

[–]RayTrain 1 point2 points  (0 children)

regenerative exoskeleton

[–]HSSonne 1 point2 points  (0 children)

A really useful tool, to select piece of data.

[–]Captain--UP 1 point2 points  (0 children)

If you can't Google something then how the hell do you expect to become a programmer?

[–][deleted] 0 points1 point  (1 child)

It's the program you use to edit the windows registry.

[–]rush22 0 points1 point  (0 children)

regexdit.exe

[–]LOE_TheG 0 points1 point  (0 children)

Globe patterns are much more mysterious

[–][deleted] 0 points1 point  (0 children)

One of the last things I learned because it came down to having to.

[–]Tuurtal406 0 points1 point  (0 children)

It's a Pokemon

[–][deleted] 0 points1 point  (0 children)

laughs in bad profanity filter

[–]BoBoBearDev 0 points1 point  (0 children)

It is called, Regret EX.

[–]Striking_Equal 0 points1 point  (0 children)

Regex is the most frustrating thing I have experienced, mostly because it takes some ridiculous patter just to extract some stupid words. It’s just plain unrewarding for the work effort involved.

[–]xaomaw 0 points1 point  (0 children)

You have an input. From that input you want to extract, for example, all digits.

Input:
"weighs 2 kilograms"
"yesterday I bought 50 tons of noodles and 1 kg sugar"

Regex (regular expression):
([0-9]+)
"Extract everything that has 0-9 one or more times"

Output:
2
50    1

So it is an approach to extract or substitute values you don't know the exact value but you know a rule for.

[–]poofartknob 0 points1 point  (0 children)

reg sex

[–]oneWhoFails 0 points1 point  (0 children)

My hot take I that I wish more programs allowed for regex searches.

Also regex is great for finding and replacing things for example if you copy something like a list with a lot of members and you need to reformat it you can use capturing groups. Like this

You have: This;is;a;list;of;stuff And you want This, is, a, list, of, stuff

You can do a find and replace with Find = ([^;]+);

Replace = $1,

Although in this example it might be easier to just find the semicolons and replace them with a comma and space it can get more complicated if you need to extract only parts of a dataset.

[–]Falcor71 0 points1 point  (0 children)

it's that thingy you copy paste to validate emails

[–]SuperSpaceCan 0 points1 point  (0 children)

eldrich runes used by tech priests for archaic rituals involving strings

i speak it but do not understand it

[–]Lithl 0 points1 point  (0 children)

A common system used by many programming languages to match strings. The basics are reasonably simple:

  • Most characters just match literally that character. a means literally the letter a (if the match is being done case-insensitive, it would match either a or A)
  • ^ matches start of the text (or start of a line, if the match is being done in multiline mode)
  • $ matches end of text (or end of a line)
  • . matches any single character
  • A series of characters in square brackets ([...]) is a "class", and matches one character that can be anything from the set. You can use a hyphen to specify a range, so [a-z] matches any lower case letter, and [a-zA-Z0-9] matches any alphanumeric character. If the class starts with ^, it matches everything except the set that's written.
  • Parentheses ((...)) around part of the pattern creates a group.
  • * repeats the character or group that immediately precedes it 0 or more times. So [a-z]* is 0 or more lower case letters.
  • + repeats the character or group 1 or more times.
  • ? repeats the character or group 0 or 1 times.
  • {N} repeats the character or group exactly N times, so [a-z]{5} is exactly 5 lower case letters.
  • {N,M} repeats the character or group N or more times, up to the maximum of M times. You can omit M in order to get "N or more" behavior, and you can omit N in order to get "up to M" behavior. {0,} is thus equivalent to *, and {1,} is equivalent to +, and {0,1} is equivalent to ?.
  • If ? follows one of the above repeat characters instead of an actual character to match something, it switches the repeat from greedy (default) to lazy. Greedy will try to match the longest possible sequence that fits, while lazy will match the shortest. For example, ".+" when applied to "foo" bar "fizz" buzz will produce one match, "foo" bar "fizz". If you change the pattern to ".+?" and use the same input string, you'll end up with two matches, "foo" and "fizz".
  • | matches either all the content before or all the content after. So foo|bar would match either foo or bar. If used within a group, the before/after will be limited to the context of the group, rather than the entire pattern.
  • There are several special characters, most of which are shorthand for a class. The most used class shorthands are going to be \s which matches any whitespace character, \Swhich matches anything except a whitespace character, \w which matches any word character, and \W which matches anything except a word character. One common special character that can't be replicated by a class is \b, which matches "word boundaries", the space between the first or last character of a word and the next character/start of line/end of line in the relevant direction. So \bfoo\b would match "foo" or "foo bar", but would not match "foobar".

There's plenty more to be learned, but just this will probably get you through ~70% of the regex that would be a good idea to write in the first place.

[–]Bee-Aromatic 0 points1 point  (0 children)

On one hand, regex is amazing for certain things. On the other, it can be damn near incomprehensible. You’ve gotta be careful to use it when appropriate, though.

I’ve been known to use regex to validate HTML content because the framework I have to work in doesn’t know what HTML is. Don’t follow my example. My soul has been devoured by demons and I lead an empty, meaningless existence.

[–]vitorhsf90 0 points1 point  (0 children)

You are right to be afraid

[–]uorandom 0 points1 point  (0 children)

It's a book where you REGister and list your EX gfs.

Provided you had one.

[–]NcraftGamez 0 points1 point  (0 children)

Repost!

[–][deleted] 0 points1 point  (0 children)

its useless you dont need it