Questions tagged [pcre]

Perl Compatible Regular Expressions(PCRE) was initially developed as a regex engine for PERL, but grew into a library that many other languages (like PHP and Apache) use for their regex. Use with the [regex] tag and any appropriate language tags.

PCRE is an initialism for Perl Compatible Regular Expressions. It is both name of a flavor and the library that implements it and makes it available for use by other programs (e.g. , ).

Despite being designed with compatibility in mind, are not 100% compatible with regular expressions (). PCRE passes many of Perl’s regression tests, though. Comparison can be found in community-authored article on Wikipedia, as suggested by PCRE homepage.

Since POSIX regex deprecation in PHP 5.3, PCRE is PHP’s only supported regex engine.

References

2401 questions
502
votes
27 answers

Non greedy (reluctant) regex matching in sed?

I'm trying to use sed to clean up lines of URLs to extract just the domain. So from: http://www.suepearson.co.uk/product/174/71/3816/ I want: http://www.suepearson.co.uk/ (either with or without the trailing slash, it doesn't matter) I have…
Joel
  • 29,538
  • 35
  • 110
  • 138
156
votes
21 answers

How do you debug a regex?

Regular expressions can become quite complex. The lack of white space makes them difficult to read. I can't step though a regular expression with a debugger. So how do experts debug complex regular expressions?
rook
  • 66,304
  • 38
  • 162
  • 239
151
votes
4 answers

How can I convert ereg expressions to preg in PHP?

Since POSIX regular expressions (ereg) are deprecated since PHP 5.3.0, I'd like to know an easy way to convert the old expressions to PCRE (Perl Compatible Regular Expressions) (preg). Per example, I have this regular expression: eregi('^hello…
netcoder
  • 66,435
  • 19
  • 125
  • 142
137
votes
2 answers

PHP regular expressions: No ending delimiter '^' found in

I've been having some trouble with regular expressions. This is my code $pattern = "^([0-9]+)$"; if (preg_match($pattern, $input)) echo "yes"; else echo "nope"; I run it and get: Warning: preg_match() [function.preg-match]: No ending…
fingerman
  • 2,440
  • 4
  • 19
  • 24
87
votes
7 answers

Rebuild uwsgi with pcre support

When running uwsgi I got the following message: !!! no internal routing support, rebuild with pcre support !!! I already have installed pcre (I think) with the following command: sudo apt-get install libpcre3 libpcre3-dev Why am I still getting…
johnmic07
  • 1,225
  • 2
  • 10
  • 14
72
votes
5 answers

What's the technical reason for "lookbehind assertion MUST be fixed length" in regex?

For example,the regex below will cause failure reporting lookbehind assertion is not fixed length: #(?()]+)#S Such kind of restriction doesn't exist for lookahead.
wamp
  • 5,789
  • 17
  • 52
  • 82
59
votes
2 answers

Tilde operator in Regular expressions

I want to know what's the meaning of tilde operator in regular expressions. I have this statement: if (!preg_match('~^\d{10}$~', $_POST['isbn'])) { $warnings[] = 'ISBN should be 10 digits'; } I found this document explaining what tilde means:…
Keira Nighly
  • 15,326
  • 8
  • 29
  • 28
59
votes
7 answers

"vertical" regex matching in an ASCII "image"

Note: This is a question about possibilities of modern regex flavors. It's not about the best way to solve this using other methods. It's inspired by an earlier question, but that one is not restricted to regex. The Problem In an ASCII…
Qtax
  • 33,241
  • 9
  • 83
  • 121
58
votes
1 answer

How to use non-capturing groups in grep?

This answer suggests that grep -P supports the (?:pattern) syntax, but it doesn't seem to work for me (the group is still captured and displayed as part of the match). Am I missing something? I am trying grep -oP…
waldyrious
  • 3,683
  • 4
  • 33
  • 41
55
votes
18 answers

Efficiently querying one string against multiple regexes

Lets say that I have 10,000 regexes and one string and I want to find out if the string matches any of them and get all the matches. The trivial way to do it would be to just query the string one by one against all regexes. Is there a faster,more…
Sridhar Iyer
  • 2,772
  • 1
  • 21
  • 28
50
votes
5 answers

Invert match with regexp

With PCRE, how can you construct an expression that will only match if a string is not found. If I were using grep (which I'm not) I would want the -v option. A more concrete example: I want my regexp to match iff string foo is not in the string.…
Joe Beda
  • 2,743
  • 1
  • 19
  • 16
50
votes
6 answers

Unicode Regex; Invalid XML characters

The list of valid XML characters is well known, as defined by the spec it's: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] My question is whether or not it's possible to make a PCRE regular expression for this (or its…
Edward Z. Yang
  • 26,325
  • 16
  • 80
  • 110
48
votes
9 answers

preg_match and UTF-8 in PHP

I'm trying to search a UTF8-encoded string using preg_match. preg_match('/H/u', "\xC2\xA1Hola!", $a_matches, PREG_OFFSET_CAPTURE); echo $a_matches[0][1]; This should print 1, since "H" is at index 1 in the string "¡Hola!". But it prints 2. So it…
JW.
  • 50,691
  • 36
  • 115
  • 143
47
votes
8 answers

Apache installation; libpcre error

When installing Apache on Ubuntu 11.10, I get the following error: configure: error: APR not found. Please read the documentation. I followed the instructions here, then, I get the error below: configure: error: pcre-config for libpcre not found.…
Myjab
  • 924
  • 1
  • 7
  • 22
1
2 3
99 100