Questions tagged [regular-expressions]

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.

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.

92 questions
200
votes
17 answers

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 be understood?
IQAndreas
  • 2,675
87
votes
12 answers

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 which has the regular expression under…
hyde
  • 3,754
86
votes
23 answers

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 every programmer of all fields? Or it is a must…
user
  • 2,200
50
votes
5 answers

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 known road to numerous bugs. Probably, this also…
c69
  • 1,358
32
votes
7 answers

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 word looking for a match? If so, how does it work? …
lazeR
  • 439
  • 1
  • 4
  • 5
9
votes
4 answers

Guarding user input of regular expressions against attacks

I am aware of Regular Expression Denial of Service (ReDoS). Is there any reasonable way to allow users to create custom regexes while guaranteeing that they don't submit an exponentially slow pattern?
icirellik
  • 429
7
votes
2 answers

Is the negation of a regular expression still regular?

All articles (example) I read till now about regular expressions and NFAs explain three operations: sequence alternation (union) repetition (Kleene star) No one talks about negation. Is the negation not regular? Update: @RemcoGerlich What does it…
ceving
  • 391
3
votes
1 answer

Why are `]` and `}` not metacharacters in Regex when ')' is?

In Regular Expressions Quick Start, it reads Twelve characters have special meanings in regular expressions: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk…
Wizard
  • 167
3
votes
1 answer

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 included in this character class? And why not include…
Alex
  • 133
2
votes
1 answer

Regex backtracking on simple pattern

I've got this code in Mathematica that fetches the left or right side of an equation by turning it into pure text then textually getting rid of the other side through regex. I've isolated the faulty pattern and can't understand why it is producing…
Dehbop
  • 169
2
votes
2 answers

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 lines using the \ special char) I have tried to get…
Guillaume
  • 2,197
1
vote
1 answer

What would be an actual DFA/actual regex engine solve this simple regex pattern?

.*foo Academically, that is a regular expression. A string aaaaaaaaaaaaafoo will match that. Also string aaaaaaaaaaaaafooaaaaafoo will match that too. I would imagine that an NFA that solve that will have 4 states. State 0 will have an arrow going…
user4951
  • 709
  • 6
  • 14
1
vote
0 answers

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. Suppose that I have the following code.
1
vote
2 answers

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 far as I can tell, there are many types of regular…
JW01
  • 3,569
0
votes
1 answer

Is PCRE execution time primitive recursive or recursive?

In other words, is it possible to create a PCRE "regular" expression which takes Ackermann(m,m) time for an m length string?
chx
  • 373
1
2