According to Wikipedia, a regular expression (abbreviated regex or regexp) is a sequence of characters that forms a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations.

learn more… | top users | synonyms (1)

117
votes
17answers
19k views

What is meant by “Now you have two problems”?

There is a popular quote by Jamie Zawinski: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. How is this quote supposed to ...
78
votes
25answers
14k views

How far should one take e-mail address validation?

I'm wondering how far people should take the validation of e-mail address. My field is primarily web-development, but this applies anywhere. I've seen a few approaches: simply checking if there is ...
78
votes
18answers
5k views

How do you learn Regular Expressions? [closed]

I'm not asking where to learn. I've found lots of good resources online, and books etc. But how the heck do I tackle them. Where is the start of it, the end? When does the regexp processor advance on ...
67
votes
23answers
11k views

Is it a must for every programmer to learn regular expressions?

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 ...
52
votes
10answers
5k views

Readable regular expressions without losing their power?

Many programmers know the joy of whipping up a quick regular expression, these days often with help of some web service, or more traditionally at interactive prompt, or perhaps writing a small script ...
36
votes
0answers
4k views

What does the Jamie Zawinski's quotation about regular expressions mean? [duplicate]

In a message to comp.emacs.xemacs, Jamie Zawinski once said: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. I've always ...
31
votes
4answers
5k views

When you should NOT use Regular Expressions?

Regular expressions are powerful tool in programmer's arsenal, but - there are some cases when they are not a best choice, or even outright harmful. Simple example #1 is parsing HTML with regexp - a ...
19
votes
3answers
2k views

Are regular expressions a programming language?

In the academic sense, do regular expressions qualify as a programming language? The motivation for my curiosity is an SO question I just looked at which asked "can regex do X?" and it made me wonder ...
15
votes
7answers
1k views

Should I refactor large functions that mostly consist of one regex? [closed]

I just wrote a function that spans approximately 100 lines. Hearing that, you are probably tempted to tell me about single responsibilities and urge me to refactor. This is my gut instinct as well, ...
13
votes
7answers
5k views

How do regular expressions actually work?

Say you have a document with an essay written. You want to parse this essay to only select certain words. Cool. Is using a regular expression faster than parsing the file line by line and word by ...
13
votes
7answers
731 views

What is more standard shortening for “regular expression”, “regex” or “regexp”? [closed]

I want to know what is more standard way to call regular expression in short way. I have seen both, regex and regexp. Google says regex has almost 2 as much hits that regexp, but it depends on way I ...
13
votes
10answers
975 views

Inspiring problems to show off the importance of regular expressions? [closed]

I am planning to give a presentation/demonstration on regular expressions at work to encourage young developers to add this powerful and important tool in their toolbox. Just teaching syntax doesn't ...
12
votes
6answers
528 views

What can / should be done with regular expressions?

I've recently learnt regular expressions and I love writing / using them. I'm looking for ideas and more opportunities to use them, however I don't want to overuse them as an all purpose tool, as ...
10
votes
5answers
5k views

Can the csv format be defined by a regex?

A colleague and I have recently argued over whether a pure regex is capable of fully encapsulating the csv format, such that it is capable of parsing all files with any given escape char, quote char, ...
8
votes
10answers
1k views

A better way of doing Regex? [duplicate]

I really dislike regular expressions, each time I come back to it I seem to have to relearn it. It's also incredibly hard to maintain, modify and at a glance understand what it is doing. Has anyone ...
7
votes
5answers
909 views

Lexical Analysis without regular expressions

I've been looking at a few lexers in various higher level langauges (Python, PHP, Javascript among others) and they all seem to use regular expressions in one form or another. While I'm sure regex's ...
5
votes
2answers
351 views

Python flavor of regular expressions - related to which?

So my copy of the classic book, Mastering Regular Expressions, just arrived, and I'm scanning through it. The cover (third edition) says, "for Perl, PHP, Java, .NET, Ruby, and More!" Well, it does ...
5
votes
3answers
346 views

Constructing a Finite State Automaton

I have an exam question that I am unsure of the answer. The question is: In organisation X valid user names have the following structure. The user name can be either the employee’s name followed ...
4
votes
2answers
934 views

Using lookahead assertions in regular expressions

I use regular expressions on a daily basis, as my daily work is 90% in Perl (legacy codebase, but that's a different issue). Despite this, I still find lookahead and lookbehind to be terribly ...
4
votes
2answers
302 views

Why does Java define homogeneous methods in Matcher and Pattern classes

I wonder why java defines homogeneous methods in two distinct classes: java.util.regex.Pattern and java.util.regex.Matcher. For example split method defined in Pattern class and replaceAll method ...
3
votes
1answer
726 views

can a regex search engine for the web be done and if so, how?

I think it would be a great idea if we had a regular expression search engine, but can it be done? It seems impossible since there's no feasible way of indexing such a huge amount of data properly and ...
3
votes
1answer
179 views

How to create an algorithm for determining Boolean Tautologies

So i am right now exploring some topics in a proof course and it occurred to me to try to create a boolean tautology solver. I would like an algorithm that is more efficient than brute force. Problem ...
3
votes
1answer
793 views

Constructing a Deterministic Finite State Automaton for a given Regex

I have a couple of exam questions for my compilers class and wanted to check if my solutions are correct. The first question is: Consider a language in which numbers start with an optional minus ...
3
votes
1answer
103 views

Why does the regular expression metacharacter \w include underscores?

In most, if not all implementations of regular expressions, the \w metacharacter matches all alphanumeric characters plus the underscore. Historically speaking, why was the underscore character ...
3
votes
2answers
112 views

Need overview of concepts and tools to translate a DSL to regular expressions

I'm looking for a little guidance. Until this morning, this was all over my head. After spending today researching Wikipedia, StackOverflow, etc., I'd say I've got my nose above the water. I'm tasked ...
3
votes
1answer
1k views

Java regex patterns - compile time constants or instance members?

Currently, I have a couple of singleton objects where I'm doing matching on regular expressions, and my Patterns are defined like so: class Foobar { private final Pattern firstPattern = ...
3
votes
1answer
266 views

Object oriented parser model review

First time I make use of Code Review. At the moment I'm creating an Android application and the company I'm developing it for, has a specific file format that can be used to give text based data ...
3
votes
1answer
99 views

Features of a routing system

I have been tinkering with the idea of creating a routing system as a learning experience and not for some production purpose. I have trying to decide what features are a must have and what features ...
3
votes
1answer
332 views

Are there any implementations of deterministic regular expressions? [closed]

The question itself is already in the title, so here I will just provide additional details. I call a regular expression "deterministic", if, after converting the regular expression into a ...
2
votes
1answer
106 views

How does the ? make a quantifier lazy in regex [closed]

I've been looking into regex lately and figured that the ? operator makes the *,+, or ? lazy. My question is how does it do that? Is it that *? for example is a special operator, or does the ? have ...
2
votes
4answers
404 views

Maintaining Regex in a .net application

I am sure everybody has had his/her share of regex woes. We have a big application with around 30 developers working on a mvc3 web application , and the two fold regex validations (front-end and ...
2
votes
3answers
399 views

Domain-specific language for text search/processing?

I work for an organization that does a lot of work with government data. We have a couple of different projects where we've abstracted out common text search/manipulation operations into reusable ...
2
votes
2answers
200 views

Regular Expressions and Language Generators

I have few CS textbooks with me which discuss languages, well actually 2 plus old course notes supplied a few years ago. I have been searching the web too any only seem to come up with vague responses ...
2
votes
1answer
409 views

How to choose a proper parser generator for PHP

Some programmers avoid regexes in some situations (see this popular @nickf comment), perhaps using a parsing framework such as Lex/Yacc. Others prefer to stay within PHP, perhaps using regular ...
2
votes
1answer
371 views

Optimized Special Character Escaper vs Matcher/Pattern

I need to escape special characters which are sent to apache lucent. Since the code will run on a production server I want the code to be the fastest possible. I've seen multiple ways to do it: ...
2
votes
1answer
82 views

String patterns that can be used to filter and group files

One of our application filters files in certain directory, extract some data from it and export a document from the extracted data. The algorithm for extracting the data depends on the file, and so ...
2
votes
2answers
121 views

What is the best strategy for transforming unicode strings into filenames?

I have a bunch (thousands) of resources in an RDF/XML file. I am writing a certain subset of the resources to files -- one file for each, and I'm using the resource's title property as the file name. ...
1
vote
2answers
97 views

Writing a simple code validator

I know that programming languages can be defined in EBNF which can be converted into regular expressions. Right now I am working on a very simple BASIC interpreter for a project. The code has to be ...
1
vote
4answers
1k views

Regex to String generation

Let's say that we have a regex and an index i. If we suppose that the set of strings that match our regex are sorted in a lexicographical order, how could we get the i element of this list? Edit : I ...
1
vote
2answers
278 views

Documentation often ommits to specify which flavour of regular expression to use, so is there a default flavour that we should all be familiar with?

Often I come across documentation that says "use a regular expression here" I have to spend quite some time digging around trying to work out which regular expression format they are expecting. As ...
1
vote
2answers
326 views

Regexp or custom parsing algo?

I was trying to parse a java properties file to avoid some Properties loading pitfalls (non ordering, loss of comments). (Property are of the form 'key = value' where the value can span on multiple ...
1
vote
1answer
53 views

Understanding of concatenation, union and iteration at regular language

I'm trying to understand what a regular language is. There are 3 compound regular expressions for regular language: concat {AB}, union {A+B} and iteration {A*}. When they are used in a simple ways ...
1
vote
1answer
308 views

pcre case insensitivity: handle escape sequences as raw bytes?

Consider the following regex (no unicode): Example:\x04\x05\x41 Suppose you search this regex case-insensitively. Would you expect it to search the final \x41 in case-sensitive manner? People whom ...
0
votes
3answers
381 views

Can regexp be used to check for palindromes?

Can regexp pattern matching be used to check for palindromes within a given text in Java? My intuition is that in order to check for palindromes, we may need to remember the character that was ...
0
votes
2answers
202 views

How should a website validate a users mailing address?

This is for a site that relies on shipping items via UPS or FedEx. I know there is software out there that does it (http://en.wikipedia.org/wiki/Coding_Accuracy_Support_System), but if you are trying ...
0
votes
1answer
122 views

How to choose between different ways to write some code?

I would like to validate point in a two-dimensional rectangular Cartesian coordinate. I found following regular expression solution on stackoverflow ^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$ . Another ...
0
votes
1answer
67 views

Nomenclature for wildcard vs regex expressions [closed]

When I see the term pattern in a description of syntax I infer a regular expression is being specified. Is there a separate term that specifies a wildcard expression? ^foo.*(bar|baz){3} <-- ...
0
votes
2answers
333 views

Multi regex matching — what to do to optimize it?

I have multiple regexes matching an input in greedy-first mode. All regexes are already compiled and I don't see what I can do more to speed-up things. I mean using tools coming with .Net framework. ...
0
votes
1answer
832 views

Why nginx's http parser doesnt use regular expressions?

I see the http parser written by Igor Sysoev for nginx does not use regular expressions https://github.com/joyent/http-parser What could be the main reason for such design decision? I guess I could ...
0
votes
0answers
24 views

Simplifying backreferences

This is a theoretical example for simplicity's sake. I use flat values in the regex to make it easily legible for almost anyone, since lots of {,(,[,\'s all jammed together can be a headache to read. ...