Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am new to programming, and at an interview I got a question on regular expressions; needless to say I couldn't answer. So I was wondering whether I should learn regular expression? Is it a must for every programmer of all fields? Or it is a must for programming for some particular fields?

Related questions:

share|improve this question
50  
I personally know what they are, when to use them and how to google syntax. shrugs –  Tyanna Feb 8 '12 at 16:46
29  
It is a must for all the power users. And programmers have to be power users in order to be productive. –  SK-logic Feb 8 '12 at 16:53
7  
Why wouldn't you want to learn them? You've already encountered at least one situation where you would've benefited from knowing them. Doesn't it stand to reason that you may find yourself in a similar situation in the future? –  FishBasketGordo Feb 8 '12 at 17:02
19  
Only those that work with text :) –  Steve Jackson Feb 8 '12 at 17:06
22  
show 16 more comments

23 Answers

up vote 97 down vote accepted

Regular expressions are such an incredibly convenient tool, available across so many languages that most developers will learn them sooner or later.

For an interviewer, they are a nice way to probe experience during an interview. If you are interviewing someone claiming years of experience who doesn't understand them, you need to dig further.

share|improve this answer
3  
I agree, it is a good question for any experienced developer. But asking a fresh graduate with no working experience about them is probably slightly unfair - unless they really wanted to filter out applicants with no practical programming experience at all. –  SK-logic Feb 8 '12 at 16:57
2  
@SK-logic: Depends on the school. We all had a mandatory course in 2nd year that made pretty heavy use of regular expressions (I admit, that's where I got hooked on them ;) ). And more than a few senior-level courses brought them up again, though never quite as intensely. –  FrustratedWithFormsDesigner Feb 8 '12 at 17:03
12  
I am a mathematician, so I have no formal background in computer science, but I would have thought that regexes were actually taught in college courses, while discuting grammars, parsers, automatas and whatnot. I actually expected that someone fresh out of college would have a better knowledge that an experienced programmer with no CS studies. :-/ –  Andrea Feb 8 '12 at 21:05
7  
@Andrea, when I was in university (BS in Computer Science) regular expressions were indeed covered when learning about grammars and automata. However, it was only the formal concept that was covered, not applications. We didn't learn the POSIX or Perl-compatible regular expression syntax, or how to use them in our coding. We learned how to draw a state diagram of a finite-state automaton, write that as a regular expression, etc. Greek letters were involved, not grep and text files. –  Adam Jaskiewicz Feb 8 '12 at 21:25
show 7 more comments

Regular expressions are a tool. It happens to be a very useful tool, so many people choose to learn how to use it. However, there's no "requirement" for you to learn how to use this particular tool, any more than there is a "requirement" for you to learn anything else.

share|improve this answer
17  
This is as true as saying that understanding boolean logic is useful, but not required. –  Oded Feb 8 '12 at 16:54
1  
@Oded: I've not found any use for the boolean logic taught in my course. –  DeadMG Feb 8 '12 at 17:29
13  
@DeadMG, I use boolean logic all the time, such as in conversions: if (!(foo && bar)) => if (!foo || !bar). Maybe you've been using it and haven't realized, or maybe you haven't been taught the right material. –  zzzzBov Feb 8 '12 at 17:55
6  
Like version control? Or software testing? –  Konrad Rudolph Feb 8 '12 at 19:18
2  
@ruakh - I agree that they're extremely useful; I use them myself all the time. I think all programmers should learn how to use regexes at some point in their career. The question, however, didn't ask that. –  eykanal Feb 8 '12 at 19:26
show 6 more comments

Regular expressions are a very terse way to express how to match patterns in text.

The requirement to parse and extract data from text or to validate that some text conforms to a specific pattern comes up very often in programming, so I would say that yes, it is important to learn about them and understand them.

They are a good tool to have in your toolbox and I would expect any experienced programmer to know how to use them.

One of the best resources to learn about regular expressions is Jeffery Friedl's book, Mastering Regular Expressions. It is rather advanced so you may want to actually read it when you have had some more experience.

You can start with the tutorial at regular-expressions.info.

share|improve this answer
1  
This is to me a matter of taste. I've never really had the need for them. I've used them on a few projects, so I know how they work but I could easily have gotten on without and have seen them misused more often than I've felt a need for them. To simple a pattern and you might as well use string manipulation to complex and it's faster to use (e)BNF at least if you like me are used to working with parsers –  Rune FS Feb 8 '12 at 19:59
add comment

regular expressions? Hmmm... sometimes is it usefull to know them but most time you allways use the same. I have used regex quite often but I woulnd't say that I am good with it. I don't like it at all and I think there are more important things to know than regex.

But in validation of formulars or data its very usefull. I think allmost every professional form is validated with regex. ASP.NET using it.

But at all: Try to do an expression when you need it and save it. You will use them hopefully all more than once. But dont waste your time with RegEx!

share|improve this answer
add comment

I would not be surprised if, for example, a games programmer or a programmer at the LHC never learned regular expressions. I might even give a games programmer a pass on not knowing SQL.

But, if you are working in information systems of any sort, and if you do not know regular expressions, you are doing yourself a disservice.

On the flip side, I wouldn't expect your standard IS programmer to know the matrix mathematics that a games programmer would. There are distinct disciplines of programmer, however must of us fall under the "information systems" umbrella.

share|improve this answer
1  
I agree, but if I were interviewing for a games programmer position I would still hire someone if they didn't know what a regular expression was. If I were hiring someone in a traditional business programming role who didn't know what regular expressions were, they would probably not be hired. –  Jonathan Rich Feb 8 '12 at 17:12
7  
Particle physicists mostly work in unix-alike environments, but like everything else computer related in that community most skills are transmitted by oral traditions. Levels of sophistication in regexps vary. But we don't search the data with regexps: there is too much of it and it's not in text formats. We search logs and ad hoc DBs and code and half-assed documentation, just like everybody else. –  dmckee Feb 8 '12 at 20:14
1  
@FrustratedWithFormsDesigner, I wouldn't hire a games progrmmer that didn't know regex. Programming is by its very nature information-intensive regardless of the field. Doesn't matter what you do -- in any discipline you have source code, regex-capable text editors, and log files. Without regex you will be significantly less efficient in performing certain tasks (even if you don't need to write a single line of regex in the shipped game). I'm not saying regex is necessary for a games programmer. I'm just saying I wouldn't hire one that didn't know it. –  Ben Lee Feb 13 '12 at 20:11
show 5 more comments

to quote another answer :

Regular expressions are a very terse way to express how to match patterns in text.

So if this is an important part of your job there is probably a better way to design the system you are developing. Unless lots of text is domain specific to your area (eg bioinformatics).

I have worked on three different enterprise systems (at three different companies over ten years) and I've written them less than five times, and that includes copy and pasting a basic e-mail validator twice.

share|improve this answer
2  
Lots of text is always domain specific. You are a programmer after all. Text (in the form of source code) is your domain. Efficiently manipulating text with regular expressions makes you a more efficient programmer. –  quanticle Feb 8 '12 at 20:35
2  
@quanticle that doesn't make any sense. You actually manipulate your source code with regular expressions ? That sounds like an absolute nightmare. Regular expressions are part of your code and your code interacts with domain specific data. –  NimChimpsky Feb 8 '12 at 21:21
3  
I do manipulate source code with regular expressions. Refactoring large chunks of code by hand is tedious and error-prone. Using an editor that supports regular expression find and replace (hint: pretty much every editor supports regexp find and replace) makes refactoring much much easier. –  quanticle Feb 8 '12 at 22:37
3  
@Dunk he's not saying that it is a large efficiency gain, over the life of a project, but rather that it can be a large gain over a small time period. I have also done refactoring where there were hundreds, even thousands, of changes in dozens of files. Over the life of the project the hours saved are insignificant, but I surely didn't want to take the hours or days it would have taken to do the same changes by hand. Modern IDE's can do some of this for you without you writing a regex, butsometimes it can't (rewiring a common assigment pattern for instance). –  jmoreno Feb 9 '12 at 6:20
2  
@NimChimpsky Regexps work everywhere. IDEs and refactoring tools only work for specific languages. For example, will your refactoring tool allow me to go through all of my templates and find/delete a string that looks like a phone number (sites.google.com/site/steveyegge2/…)? –  quanticle Feb 10 '12 at 23:01
show 4 more comments

It depends on the area/platform you are working in

  1. For desktop application, you can live a kool life without knowing anything about regular expressions. But mind you, Visual Studio has regular expression search but I wonder if anyone use it on regular basis. I am thinking not (you have to fix your regular expression first because you can make a search :) ).

  2. As a web developer you will most likely have to learn Regular Expression. You can get away with it because you can easily find code snippet online that will suffice you need but learning will help.

There are areas like documentation, litigation, legal stuff where regular expression is an indispensable tool. You have to know it. If you don't you simply wont get the job.

So in short, if it is not part of a job description, don't even bother about it. If you like to learn it, learn it for fun.

share|improve this answer
add comment

Regular expressions can be thought of as a very useful, cross-language, cross-platform, processing tool. I can use regex in my code, I can use regex in my editor, I can use regex on the commandline!

It's not just that you need, or should learn them. It's more like

WOW! I LOVE REGEX!

share|improve this answer
add comment

Regardless of the domain you are working in, regular expressions are a useful tool to know because most programming languages are written as plain text. Regex is therefore a great way to manipulate and refactor your source code and it is built into many text editors. I have seen countless programmers making repeated changes to source files when a regular expression would make the changes much more quickly and accurately.

This is what Chapter 3 of the Pragmatic Programmer talks about with plain text being the "basic raw material of programming".

share|improve this answer
1  
If I had asked the question in this post, I would have given you the check mark. This is the single most important reason I would never hire someone who didn't know regex. It's a powerful code editing and log searching tool even if you never need to write a single line of code that uses it. Someone that doesn't know it is going to be orders of magnitude less efficient at certain daily tasks. –  Ben Lee Feb 13 '12 at 20:16
show 1 more comment

You don't need to know all the little regex quirks by heart in order to be a great programmer, specially since they all tend to vary a bit around differnt implementations and programming languages. What you do need to know is

  • What are regular expressions and regexes.
  • What kind of things they do efficiently (parsing regular languages)
  • What kind of things are inneficient (parsing nested stuff, using tons of backreferences)
  • Where to get information on the syntax whenever you need to read or write one.

Anyway, you don't even have to worry to much about wether it will take too much effort to learn them in the first place. The simplest operators (., *, |, (), etc) are almost universally present and go a long way!

share|improve this answer
show 1 more comment

Regular expressions are very useful tool, and there are a lot of situations, when programmer should use them. You shouldn't learn them all by heart to use. Just use reference and do your task. After 10-20-50-100(depending on the programmer) tasks where you have used regular expressions, you will know them all by heart. They are being learned by themselves, you shouldn't learn them specially.

share|improve this answer
add comment

I work in an embedded code base that does not have a regular expression library available. A certain task required 275 lines of code, and took around two weeks to debug all the corner cases, back and forth between tester and developer. I later wrote a function to do the same thing as part of a javascript utility. The exact same task using regular expressions used 10 lines of code and worked perfectly in around 15 minutes.

Do you need regular expressions? Technically not, but it's foolish to intentionally remain ignorant of a tool that efficient.

share|improve this answer
add comment

In contrast to the majority of the answers here I don't think knowledge of reg exs is a required skill to be a productive programmer. When I interview candidates for a position I would dig deeper if they felt compelled to bring their skills of reg ex up them self. Why? they are often used in places because they like the right hammer but often what you needed was a screw driver. Search SO for HTML and regex and you'll see quite a few questions and quite a few reasons why regex is a misfit.

The ability to do proper OOD is required before I will advocate for hiring an interviewee the knowledge of regular expressions is certainly not. And I don't actually think any one believes you can be are more productive programmer knowing regexs but lacking in areas such as analysis, design and knowledge of the framework used

Sure sometimes they are handy but in my more than 20 years as a professional developer I think I've used them less than 20 times in code (and I've even code a few perl scripts) I do use them regularly to do search and replace though.

share|improve this answer
show 1 more comment

As already mentioned Regex is a tool, a very useful tool IMHO.

Consider this example:

//fomat number using string.replace and regex in javascript
function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
} 

Just one line to convert for example a number like this 1000000.00 into "1,000,000.00"

Using any other method will be far more complex

share|improve this answer
2  
and this isn't complex? this uses several special features not in the standard regex set. a simple length and fill would do just fine for this –  ratchet freak Feb 8 '12 at 22:39
add comment

I would really advise you to learn regexps. At work, when we are hiring programmers, the candidates who don't know regexp are generally quickly disposed of. Not because they absolutely need to know regular expressions, but because it is a very good indicator of their level of understanding of programming and level of programming addiction.

A real programmer knows their regular expressions

This is a little biased, however, as I'm mostly involved in web programming, where regular expressions are something you are bound to come across a lot, both on the server side and on the client side. If you were an assembly programmer for a built in system in a dishwasher, you would probably not come across regular expressions at all. But it would still possibly come in handy, as you are in most cases more able to wield your development environment when you do know regular expressions (search and replace, search for a file, search in multiple files, etc...).

Also, you will find it easier to be acknowledged by fellow programmers if you do know it.

share|improve this answer
show 5 more comments

Jeff Atwood wrote a great blog about regular expressions, in it there is this amazing quote:

Some people, when confronted with a problem, think "I know, I'll use regular 
expressions." Now they have two problems. [Jamie Zawinski]

There is also a great answer to a good question a stack overflow you might want to check out.

You should definitively know a little about it. Regular expressions are easy to learn but difficult to master. You don't need to learn everything about it to start using it. Just be sensible about it.

 

tl;dr

You need to know about the technology so that you can make a good decision on when to use it.

share|improve this answer
add comment

Is it a must? You already know what you MUST know: that they exist, what they are, and what they are used for. Your job as a programmer is to solve problems. You now know enough to begin factoring them into your solution for solving your problem. Should you learn regexs? Absolutely. Priority is up to you...there are jobs where they will be used everyday, and jobs where they will never be used. Simple guide would be how much pattern matching the problems you expect to encounter will require.

share|improve this answer
add comment

I see many answers to this already, but they all seem to be about the merits of knowing regular expressions as a tool in the programmer's toolbox.

I much prefer to think of regular expressions as the kind of knowledge that expands one's understanding of how computers work. Once someone truly gets what a regular language is, and that you can express it with three simple operations, I believe it gives them something beyond simply a tool for validating simple strings.

They become able to better understand parsing in general, which is useful to just about every kind of programming, and better understand how the tools we use on a regular basis (compiler, editor, browser, etc.) work.

The practical benefits of using regular expressions is of course enormous. Even if you don't "program" with them - I use regular expressions doing find/replace in my editor all the time, and measure how much more productive they make me.

So, while it is not necessary, I think it is one of the first tools a programmer should learn.

share|improve this answer
add comment

It is not a must. You just need to be aware of their existence and when to use them. Syntax specifics are easy to find and get right using google and online tools.

share|improve this answer
add comment

Hmm, I need to comma-separate and output this list of strings, let me write function containing a for loop that takes a separator argument, and keep appending them together...or I could just use this existing 'join' command.

I need sort to these complex objects based on a couple of attributes that each object has, let me remind myself how to write a sort function that does that, oh no wait, I can just use the existing standard sort function that this language supports. I'll have to learn how to write a custom comparator but it shouldn't be too hard, it's obviously better than maintaining my own sort function.

I'm a bit scared by the idea of virtual methods, but it should help me reduce these lists, each one for a different type of item, down to a single list. Then I don't have to write half a dozen similar statements processing each list in a different way. Guess I ought to study hard and learn those things because my code will be a lot simpler and cleaner if I do that.

I need to extract from this text file these pairs of words and values amongst all this other noise, let me write a fifty-line parser to read the characters one by one and if one of the characters look like the start of what I'm looking for I'll set this state variable and start parsing it differently and so on etc... of course my case is obviously so special no-one else has come across an issue like this before and invented a generic solution that takes one line of code to do it. That reminds me I ought to take those programming books I never read to the second-hand book store.

share|improve this answer
add comment

Short Answer: No But.... knowledge is power.

I'm a web developer and i find any Regular Expression i generally need has already been written for me. However i have had problems with copying and pasting and not realizing what it was doing or not, which is a danger of copying and pasting ANYTHING.

One example: An email regex i copied and pasted didn't allow for a period or plus in an email which is allowed in the email address spec. In fact many people use gmail with actualemail+sitename@gmail.com so that it becomes easy to filter 'sitename' should they choose to spam.

share|improve this answer
add comment

Rob Pike doesn't think so:

Regular expressions are hard to write, hard to write well, and can be expensive relative to other technologies. Standard lexing and parsing techniques are so easy to write, so general, and so adaptable there's no reason to use regular expressions.

Another way to look at it is that lexers and parsing are matching statically-defined patterns, but regular expressions' strength is that they provide a way to express patterns dynamically. They're great in text editors and search tools, but when you know at compile time whatall the things are you're looking for, regular expressions bring far more generality and flexibility than you need.

Encouraging regular expressions as a panacea for all text processing problems is not only lazy and poor engineering, it also reinforces their use by people who shouldn't be using them at all.

It's worth noting that Pike acknowledges they have a place, of course, and IMHO, regular expressions are a pretty powerful tool, one that's absolutely rewarded the time I've spent learning to use, and from that standpoint, I'd absolutely recommend learning them. But it's possible the real issue is that they're a band-aid which has let me get away with knowing less about lexing and parsing than I should. :)

share|improve this answer
add comment

Let me just say, Regular Expressions are often the right tool for the job and are quite handy, especially with applications that do a lot of text processing. However, memorizing them is madness. I have a cheat sheet pinned up on my wall that has all of the special characters, anchors, character classes, pattern modifiers, metacharacters... Just spend the 20 minutes it takes to learn how to use them and get a cheat sheet to keep track of everything else. All you need them for is to be able to write a regular expression when you need one, which is usually about every 6 months--about as often as you come across a problem where they are the best tool to use for the job. Unless you are on the Perl, Latex, Emacs or some other heavy text processing team.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.