I am new to programming and got a question on regular expressions in an interview. Needless to say, I couldn't answer. Should I learn regular expressions? Is it a must for programmers of any field or only for programmers in particular fields?

Related questions:


23 Answers 23


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.

  • 4
    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
    Commented Feb 8, 2012 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. Commented Feb 8, 2012 at 17:03
  • Agreed with comments above - I would not expect someone fresh out of school to necessarily know regular expressions. But I might still ask.
    – ptyx
    Commented Feb 8, 2012 at 18:19
  • 14
    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
    Commented Feb 8, 2012 at 21:05
  • 8
    @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. Commented Feb 8, 2012 at 21:25

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.

  • 19
    This is as true as saying that understanding boolean logic is useful, but not required.
    – Oded
    Commented Feb 8, 2012 at 16:54
  • 1
    @Oded: I've not found any use for the boolean logic taught in my course.
    – DeadMG
    Commented Feb 8, 2012 at 17:29
  • 16
    @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
    Commented Feb 8, 2012 at 17:55
  • 7
    Like version control? Or software testing? Commented Feb 8, 2012 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
    Commented Feb 8, 2012 at 19:26

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 (IS) 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 most of us fall under the "information systems" umbrella.

  • 1
    Programmers at the LHC might use regular expressions to search through data files produced by sensors. Games programmers might use regular expressions to search the log files produced by their games. Ok, they might not have to, but it could probably make such tasks a lot easier. Commented Feb 8, 2012 at 17:08
  • 2
    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. Commented Feb 8, 2012 at 17:12
  • That's fair, I guess. Commented Feb 8, 2012 at 17:21
  • 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. Commented Feb 8, 2012 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
    Commented Feb 13, 2012 at 20:11

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.

  • 2
    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
    Commented Feb 8, 2012 at 19:59

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.

  • Programmers who don't know regular expressions often create a big pile of WTF when a regular expression would work very nicely. Programmers who ONLY know regular expressions often create a smaller but massively incorrect pile of WTF attempting to process non-regular languages (e.g. HTML) with regular expressions. Commented Aug 17, 2012 at 20:39
  • I highly agree with that answer. I don't remember the last time I used regex. And I've been doing web development for over 10 years. It's really waste of brain cells to keep that information around. Especially when I can google it in 10s.
    – freakish
    Commented Jul 8 at 17:05

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".

  • Why don't you just use a ide that supports refactoring ? Commented Feb 9, 2012 at 7:50
  • 2
    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
    Commented Feb 13, 2012 at 20:16
  • Literally not a single programming language, not even markup language (like XML or JSON) is parsable through regex. Applying regex to source code more complicated than 20 loc is pretty much always a failure that needs manual fixing. I have no idea why this answer is so highly upvoted.
    – freakish
    Commented Jul 8 at 17:00

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.



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


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.


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.

  • 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
    Commented Feb 8, 2012 at 20:35
  • 3
    @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. Commented Feb 8, 2012 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
    Commented Feb 8, 2012 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
    Commented Feb 9, 2012 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
    Commented Feb 10, 2012 at 23:01

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



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!

  • I think this is a really good point. Knowing how to use back-references and other advanced features of some RE engines is only moderately useful. In many/most cases, combining the use of REs with a little bit of logic is more readable and easier to maintain. Commented Feb 8, 2012 at 23:15

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

  • 3
    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 Commented Feb 8, 2012 at 22:39

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.


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.

  • Also, it is futile to learn the complete specs by heart in the beginning, as there are a lot of different dialects. Instead, try to focus on learning the basics: (1) craft a few simple regexps and learn the meaning of . * + {} [] () and - and a few basic classes. (2) with a given spec at your hands, try to understand what is happening in the regexpes you come across.
    – Xyz
    Commented Feb 8, 2012 at 20:27
  • What do you use reg exps for in yr web dev. Its what I do and I have used like 5 times in 10years+. Commented Feb 8, 2012 at 21:26
  • For a starter: Parsing or validating user input (e.g. e-mail, zip code/postal code, user search forms). But also for I18n and l10n purposes for custom and variable translation strings in multiple languages/locales. And they have sure come the help with some error detection & quite a lot of error handling. And in the CMS/blogs/comments world for wiki/forum markup. For external data (user input, scraped pages, synched resources, etc.) parsing, handling and rewriting of content/links/whatever. Plus for all a lot of stats purposes and loggings (browser detection, referer sorting, ...)
    – Xyz
    Commented Feb 8, 2012 at 21:49
  • -1 for such an insanely ignorant comment "A real programmer..."
    – Dunk
    Commented Feb 8, 2012 at 23:24
  • Its a rather superficial way of validating user input and I woul dbe surpised if your regex's were 100% accurate (email validation is the famous one). And I don't grok this "custom and variable translation strings in multiple languages/locales" you translate between languages using regex's - er wtf ? Stats purposes - you do statistics with regular expressions ? Rewwriting links/content with regexs sounds like a code smell to me. Commented Jun 19, 2013 at 13:23

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.


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.


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 [email protected] so that it becomes easy to filter 'sitename' should they choose to spam.


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!


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.


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.


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.


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. :)


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.

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