I saw devs mentioning about Regular expressions time to time here. And, some says it is hard. What are the most regular usages of this?
For further actions, you may consider blocking this person and/or reporting abuse
I saw devs mentioning about Regular expressions time to time here. And, some says it is hard. What are the most regular usages of this?
For further actions, you may consider blocking this person and/or reporting abuse
Ashwin Kumar -
Mike Young -
Mike Young -
Mike Young -
Top comments (2)
Regular expressions are, in essence, a pattern matching language specifically for finding stuff in textual data. They get used all the time in a wide variety of programming situations because they're generally fast (if implemented correctly in the underlying language) and a lot more flexible than manually prefix or suffix matching and splitting strings.
There are a couple of things that make them challenging:
Some quick examples, in multiple formats (shown as they're most likely to be seen in that context):
Basic match for email addresses (lots of false positives and a few false negatives because email addresses are complicated):
/\S\+@\S\+/
re.compile("\\S+@\\S+")
/\S+@\S/
/\S+@\S+/
or"\\S+@\\S"
depending on the implementation.grep
):[^[:space:]]\+@[^[:space:]]\+
Match only the last word of every line/string (by matching on word boundaries and the end of the line/string):
/\<.\{-1,}\>$/
re.compile("\\b.+?$")
/\b.+?$/
/\b.+?$/
or"\\b.+?$"
You can find a cheatsheet for the more common syntaxes here, and info for JS here.
It's like any tool, if you know it it helps you a lot but if you don't then you don't realize what you miss.
I remember my early coding days when all I know was antiquated languages like BASIC or PHP and I spent my time doing
explode
/strpos
. Now I just fire regular expressions for any occasion and it feels really simple.I believe it's hard for several reasons:
Related to usage, I'm currently developing a regular expression engine that handles other things than just strings. So I'd say that usage is anything that involves looking at patterns in a stream of data, be it validating an email address or putting together a sequence of UDP messages.