Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are good regular expressions?

Tags:

regex

I have worked for 5 years mainly in java desktop applications accessing Oracle databases and I have never used regular expressions. Now I enter Stack Overflow and I see a lot of questions about them; I feel like I missed something.

For what do you use regular expressions?

P.S. sorry for my bad english

like image 325
Telcontar Avatar asked Aug 07 '08 16:08

Telcontar


People also ask

What are regular expressions good for?

Regular expressions are useful in search and replace operations. The typical use case is to look for a sub-string that matches a pattern and replace it with something else. Most APIs using regular expressions allow you to reference capture groups from the search pattern in the replacement string.

How do you write a good regex?

If you want to match for the actual '+', '. ' etc characters, add a backslash( \ ) before that character. This will tell the computer to treat the following character as a search character and consider it for matching pattern. Example : \d+[\+-x\*]\d+ will match patterns like "2+2" and "3*9" in "(2+2) * 3*9".

What is regular expression examples?

Example 4: Solution: As we know, any number of a's means a* any number of b's means b*, any number of c's means c*. Since as given in problem statement, b's appear after a's and c's appear after b's. So the regular expression could be: R = a* b* c*

Which are 3 uses of regular expression?

Regular expressions are used in search engines, in search and replace dialogs of word processors and text editors, in text processing utilities such as sed and AWK, and in lexical analysis.


4 Answers

Consider an example in Ruby:

puts "Matched!" unless /\d{3}-\d{4}/.match("555-1234").nil?
puts "Didn't match!" if /\d{3}-\d{4}/.match("Not phone number").nil?

The "/\d{3}-\d{4}/" is the regular expression, and as you can see it is a VERY concise way of finding a match in a string.

Furthermore, using groups you can extract information, as such:

match = /([^@]*)@(.*)/.match("[email protected]")
name = match[1]
domain = match[2]

Here, the parenthesis in the regular expression mark a capturing group, so you can see exactly WHAT the data is that you matched, so you can do further processing.

This is just the tip of the iceberg... there are many many different things you can do in a regular expression that makes processing text REALLY easy.

like image 193
Mike Stone Avatar answered Oct 11 '22 17:10

Mike Stone


Regular Expressions (or Regex) are used to pattern match in strings. You can thus pull out all email addresses from a piece of text because it follows a specific pattern.

In some cases regular expressions are enclosed in forward-slashes and after the second slash are placed options such as case-insensitivity. Here's a good one :)

/(bb|[^b]{2})/i

Spoken it can read "2 be or not 2 be".

The first part are the (brackets), they are split by the pipe | character which equates to an or statement so (a|b) matches "a" or "b". The first half of the piped area matches "bb". The second half's name I don't know but it's the square brackets, they match anything that is not "b", that's why there is a roof symbol thingie (technical term) there. The squiggly brackets match a count of the things before them, in this case two characters that are not "b".

After the second / is an "i" which makes it case insensitive. Use of the start and end slashes is environment specific, sometimes you do and sometimes you do not.

Two links that I think you will find handy for this are

  1. regular-expressions.info
  2. Wikipedia - Regular expression
like image 28
Teifion Avatar answered Oct 11 '22 15:10

Teifion


Coolest regular expression ever:

/^1?$|^(11+?)\1+$/

It tests if a number is prime. And it works!!

N.B.: to make it work, a bit of set-up is needed; the number that we want to test has to be converted into a string of “1”s first, then we can apply the expression to test if the string does not contain a prime number of “1”s:

def is_prime(n)
  str = "1" * n
  return str !~ /^1?$|^(11+?)\1+$/ 
end

There’s a detailled and very approachable explanation over at Avinash Meetoo’s blog.

like image 27
Konrad Rudolph Avatar answered Oct 11 '22 15:10

Konrad Rudolph


If you want to learn about regular expressions, I recommend Mastering Regular Expressions. It goes all the way from the very basic concepts, all the way up to talking about how different engines work underneath. The last 4 chapters also gives a dedicated chapter to each of PHP, .Net, Perl, and Java. I learned a lot from it, and still use it as a reference.

like image 40
Kibbee Avatar answered Oct 11 '22 17:10

Kibbee