Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Regular Expressions a must for programming? [closed]

People also ask

Are regular expressions necessary?

First, you may notice how we are defining our string with the prefix r' this r indicates to python that we are about to use Regex. Also often called raw strings, this does not change the type of the string. It mostly changes how a string is interpreted. It's not always necessary, but its best practice.

How important is regex in programming?

RegEx allows us to check for patterns in text strings such as trying to match a valid email address or password. One of the great superpowers of RegEx is being able to define your own search criteria for a pattern to fit your needs, and it is like a language of its own.

What are the three main uses of regular expressions?

The three basic operations in which regular expressions are used are: matching (Does this (entire) string match this pattern?) searching (Is this pattern found within this string?) transforming (such as replacing one or all occurrences of a pattern with another string)


One could easily go without them but one should (IMHO) know the basics, for 2 reasons.
1) There may come a time where RegEx is the best solution to the problem at hand (see image below)
2) When you see a Regex in someone else's code it shouldn't be 100% mystical.

preg_match('/summarycount">.*?([,\d]+)<\/div>.*?Reputation/s', $page, $rep);

This code is simple enough but if you don't know RegEx then that stuff thats in the first parameter may as well be a Martian language. The RegEx thats used here is actually pretty simple once you learn the basics, and to get you that far head over to http://www.regular-expressions.info/ they have ALOT of info about RegEx and its various implimentations on the different platforms/langauges they also have a great tutorial to get started with. After that check out RegexBuddy, it can help you build RegExs and while you build them if you watch what it does then it can help you lean, it by far was the best $39.95 I've ever spent.



Original Comic


Yes. You can manage without them, but you really should learn at least the basics as most computing tasks could use them. You will save a lot of pain and hassle in the long run. Regex's are much easier than you think once you get over the initial 'wtf' stage.


I would say no, they are not a must. You can be a perfectly good programmer without knowing them.

I find I use Regular Expressions mostly for one-off tasks of data manipulation rather than for actually putting in application code. They can be handy for validating input data but these days your controls often do that for you anyway.


Not at all. Anything that you can do with regular expressions is entirely possible to do without them.

However, it's a powerful pattern matching system, so some things that is quite easy to accomplish with a simple regular expression pattern takes a lot of code to do without it.

For example, this:

s = Regex.Replace(s, "[bcdfghjklmnpqrstvwxz]", "$1o$1");

needs a bit more code to do without a regular expression:

StringBuilder b = new StringBuilder();
foreach (char c in s) {
   if ("bcdfghjklmnpqrstvwxz".IndexOf(c) != -1) {
      b.Append(c).Append('o').Append(c);
   } else {
      b.Append(c);
   }
}
s = b.ToString();

Or if you are not quite as experienced a programmer, you could easily create something that is even more code and performs horribly bad:

string temp = "";
for (int i = 0; i < s.Length; i++ ) {
   if (
      s[i] == 'b' || s[i] == 'c' || s[i] == 'd' ||
      s[i] == 'f' || s[i] == 'g' || s[i] == 'h' ||
      s[i] == 'j' || s[i] == 'k' || s[i] == 'l' ||
      s[i] == 'm' || s[i] == 'n' || s[i] == 'p' ||
      s[i] == 'q' || s[i] == 'r' || s[i] == 's' ||
      s[i] == 't' || s[i] == 'v' || s[i] == 'w' ||
      s[i] == 'x' || s[i] == 'z'
   ) {
      temp += s.Substring(i, 1);
      temp += "o";
      temp += s.Substring(i, 1);
   } else {
      temp += s.Substring(i, 1);
   }
}
s = temp;

Let me put it this way, if you have regular expressions in your toolkit, you'll save yourself a lot of time and energy. If you don't have them, you won't know what you're missing out on so you'll still be happy.

As a web developer, I use them very often (input validation, extracting data from a site etc).

EDIT: I realized it might help you to look at some common problems that regex is used for by looking at the regex tag right here on stackoverflow.


I would say yes.

They're so universally useful that it's a pretty significant handicap to be entirely without the ability to at least read & write simple ones.

Languages that Support Regular Expressions

  • Java
  • perl
  • python
  • PHP .
  • C#
  • Visual Basic.NET
  • ASP
  • powershell
  • javascript
  • ruby
  • tcl
  • vbscript
  • VB6
  • XQuery
  • XPath
  • XSDs
  • MySQL
  • Oracle
  • PostgreSQL

IDEs and Editors that Support Regular Expressions

  • Eclipse
  • IntelliJ
  • Netbeans
  • Gel
  • Visual Studio
  • UltraEdit
  • JEdit
  • Nedit
  • Notepad++
  • Editpad Pro
  • vi
  • emacs
  • HAPEdit
  • PSPad

And let's not forget grep and sed!

As an employer, which would you rather have, a good programmer that - once in a while - will have to manually find/replace some set of similar strings across thousands of source files and require hours or days to do it, or a good programmer that - once in a while - spends five, or even ten minutes crafting a regex to accomplish the same thing that runs in the time it takes them to go get some coffee?

Real World Practical Usage in this very Answer

In fact, I actually used a regex in crafting this post. I initially listed the languages that support it in comma delimited prose. I then rethought it and changed the format to a bulleted list by searching for the expression (\w+), and replacing it with \n* $1 in JEdit. And the more experience you get with them, using them will become more and more cost effective for shorter and shorter sets of actions.


No. You can be programming for years without touching regular expressions. Of course it will mean that for some cases where someone who knows RE:s would use them, you would do something else. There is always more than one way to solve a particular problem, and regular expressions is just one way (a very efficient, and perhaps therefore popular way) of expressing patterns.