Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <h2>The Stack Overflow Regular Expressions FAQ</h2>
- ---
- <H3>Table of contents</H3>
- Stack Overflow questions and answers:
- - [Quantifiers](URL): Greedy, possessive, reluctant, `*`:0-or-more, `+`:1-or-more, `{min,max}`
- - [Character Classes](URL)
- - [Special Characters, Anchors, Other](URL)
- - [Groups](URL)
- - [Lookarounds](URL)
- - [Modifiers](URL)
- - [Common Validation Tasks and Advanced Regex-Fu](URL)
- External links:
- - [Flavor-Specific Information](URL)
- - [General information](URL)
- - [Tools: Testers and Explainers](URL): Websites and applications
- ---
- [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
- to table of contents
- <H2>The Stack Overflow Regular Expressions FAQ</H2>
- ---
- <H3>Quantifiers</H3>
- - Zero-or-more: [`*`:greedy](http://stackoverflow.com/a/10764399), [`*?`:reluctant](http://stackoverflow.com/a/10764399), [`*+`:possessive](http://stackoverflow.com/a/17064242)
- - One-or-more: [`+`:greedy](http://stackoverflow.com/a/10764399), [`+?`:reluctant](http://stackoverflow.com/a/10764399), [`++`:possessive](http://stackoverflow.com/q/4489551)
- - [`?`:optional (zero-or-one)](http://stackoverflow.com/a/17400486)
- - Min/max ranges (all inclusive): [`{n,m}`:between n & m](http://stackoverflow.com/a/17032985), [`{n,}`:n-or-more](http://stackoverflow.com/a/17120435), [`{n}`:exactly n](http://stackoverflow.com/a/17829727)
- - More on the differences between greedy, possessive, and reluctant:
- - (Greedy vs non-greedy, non-greedy also known as "lazy", "ungreedy" or "reluctant")
- - [Greedy vs. Reluctant vs. Possessive Quantifiers](http://stackoverflow.com/q/5319840)
- - [In-depth discussion on the differences between greedy versus non-greedy](http://stackoverflow.com/a/3075532)
- - [What's the difference between `{n}` and `{n}?`](http://stackoverflow.com/q/18006093)
- - [Can someone explain Possessive Quantifiers to me?](http://stackoverflow.com/q/1117467) [tag:php], [tag:perl], [tag:java], [tag:ruby]
- - [Emulating possessive quantifiers](http://stackoverflow.com/q/5537513) [tag:.net]
- - Non-stackoverflow references: From [Sun](http://docs.oracle.com/javase/tutorial/essential/regex/quant.html), [regular-expressions.info](http://www.regular-expressions.info/possessive.html)
- [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
- to table of contents
- <H2>The Stack Overflow Regular Expressions FAQ</H2>
- ---
- <H3>Character Classes</H3>
- - [`[...]`](http://stackoverflow.com/a/1553171):any one character, [`[^...]`](http://stackoverflow.com/a/20802463):negated/any character but
- - [`[...-...]` / `[range-[bar]]`](http://stackoverflow.com/a/10738860):any one character in range [tag:java], [tag:ruby] 1.9+
- - [`[range&&[bar]]`:intersection](http://stackoverflow.com/a/15935168) [tag:java], [tag:ruby] 1.9+
- - [`[[:alpha:]]`](http://stackoverflow.com/a/12276342):POSIX character classes
- - [Why do `[^\\D2]`, `[^[^0-9]2]`, `[^2[^0-9]]` get different results in Java?](http://stackoverflow.com/q/21934168) [tag:java]
- - Short-Hand:
- - Digit: [`\d`:digit](http://stackoverflow.com/a/16621778), [`\D`:non-digit](http://stackoverflow.com/a/19011185)
- - Word character (Letter, digit, underscore): [`\w`:word character](http://stackoverflow.com/a/11874899), [`\W`:non-word character](http://stackoverflow.com/a/19011185)
- - Whitespace: [`\s`:whitespace](http://stackoverflow.com/a/21067350), [`\S`:non-whitespace](http://stackoverflow.com/a/19011185)
- [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
- to table of contents
- <H2>The Stack Overflow Regular Expressions FAQ</H2>
- ---
- <H3>Special Characters, Anchors, Other</H3>
- Special characters:
- - Horizontal whitespace: [`\h`:space-or-tab](http://stackoverflow.com/a/4910093), [`\t`:tab](http://stackoverflow.com/a/17950891)
- - Newlines:
- - [`\r`, `\n`:carriage return and line feed](http://stackoverflow.com/a/3451192)
- - [`\R`:generic newline](http://stackoverflow.com/a/18992691) [tag:php]
- - Other: [`\v`:vertical tab](http://stackoverflow.com/q/12290224), [`\e`:the escape character](http://stackoverflow.com/a/4275788)
- Anchors:
- - *(Also see "Flavor-Specific Information > Java > The functions in `Matcher`")*
- - [`^`:start of line/input](http://stackoverflow.com/a/22759143/578411), [`\b`:word boundary, and `\B`:non-word boundary](http://stackoverflow.com/a/6664167), [`$`:end of line/input](http://stackoverflow.com/a/22759143/578411)
- - [`\A`:start of input, `\Z`:end of input](http://stackoverflow.com/a/4020821) [tag:php], [tag:perl], [tag:python], [tag:ruby]
- - [`\G`:start of match](http://stackoverflow.com/q/21971701) [tag:php], [tag:perl], [tag:ruby]
- Other:
- - [`|`:or operator](http://stackoverflow.com/a/22187948)
- - [`.`:any character](http://stackoverflow.com/a/13594017), [`[.]`:literal dot character](http://stackoverflow.com/a/21929764)
- - [What special characters must be escaped?](http://stackoverflow.com/q/399078)
- - Control verbs ([tag:php] and [tag:perl]): [`(*PRUNE)`](http://stackoverflow.com/a/20008790), [`(*SKIP)`](http://stackoverflow.com/a/20008790), [`(*FAIL)` and `(*F)`](http://stackoverflow.com/a/20008790)
- - [tag:php] only: [`(*BSR_ANYCRLF)`](http://stackoverflow.com/a/7374702)
- - Recursion ([tag:php] and [tag:perl]): [`(?R)`](http://stackoverflow.com/q/8440911), [`(?0)` and `(?1)`](http://stackoverflow.com/a/20569361), [`(?-1)`](http://stackoverflow.com/a/17845034), [`(?&groupname)`](http://stackoverflow.com/a/18151617)
- [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
- to table of contents
- <H2>The Stack Overflow Regular Expressions FAQ</H2>
- ---
- <H3>Groups</H3>
- - [`(...)`:capture group](http://stackoverflow.com/q/21880127), [`(?:)`:non-capture group](http://stackoverflow.com/q/3512471)
- - [`\1`:backreference and capture-group reference, `$1`:capture group reference](http://stackoverflow.com/q/21880127)
- - [`\g<1>123`:How to follow a numbered capture group, such as `\1`, with a number?:](http://stackoverflow.com/q/5984633) [tag:python]
- - [What does a subpattern `(?i:regex)` mean?](http://stackoverflow.com/a/3812728)
- - [`(?>)`:atomic group](http://stackoverflow.com/q/14411818), [`(?|)`:branch reset](http://stackoverflow.com/a/5333645)
- - [Equivalent of branch reset in .NET/C#](http://stackoverflow.com/a/5378077) [tag:.net]
- - Named capture groups:
- - [tag:java]: `(?<groupname>regex)`: [Overview](https://blogs.oracle.com/xuemingshen/entry/named_capturing_group_in_jdk7) and [naming rules](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#groupname) *(Non stackoverflow links)*
- - Other languages: [`(?P<groupname>regex)`](http://stackoverflow.com/q/10059673) [tag:python], [`(?<groupname>regex)`](http://stackoverflow.com/a/20355718/578411) [tag:.net], [`(?<groupname>regex)`](http://stackoverflow.com/a/288989) [tag:perl], `(?P<groupname>regex)` and `(?<groupname>regex)` [tag:php]
- [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
- to table of contents
- <H2>The Stack Overflow Regular Expressions FAQ</H2>
- ---
- <H3>Lookarounds</H3>
- - Lookaheads: [`(?=...)`:positive](http://stackoverflow.com/a/1570916), [`(?!...)`:negative](http://stackoverflow.com/a/12210820)
- - Lookbehinds: [`(?<=...)`:positive](http://stackoverflow.com/a/11197672), [`(?<!...)`:negative](http://stackoverflow.com/a/11197672) (not supported by [tag:javascript])
- - Lookbehind limits in:
- - [Lookbehinds need to be constant-length](http://stackoverflow.com/a/22821726) [tag:php], [tag:perl], [tag:python], [tag:ruby]
- - [Lookarounds of limited length `{0,n}`](http://stackoverflow.com/a/20994257) [tag:java]
- - [Variable length lookbehinds are allowed](http://stackoverflow.com/a/20994257) [tag:.net]
- - Lookbehind alternatives:
- - [Using `\K`](http://stackoverflow.com/a/11640500) [tag:php], [tag:perl]
- - [Support of `\K` in regex](http://stackoverflow.com/a/13543042)
- - [Alternative regex module for python](http://stackoverflow.com/a/11641102) [tag:python]
- - [The hacky way](http://stackoverflow.com/a/11640862)
- - [Mimicking Lookbehind in JavaScript](http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript) <sup>External link, someone got one from SO?</sup>
- [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
- to table of contents
- <H2>The Stack Overflow Regular Expressions FAQ</H2>
- ---
- <H3>Modifiers</H3>
- - [What does the `c` modifier mean?](http://stackoverflow.com/a/11395687) [tag:perl]
- - [What does the `e` modifier mean?](http://stackoverflow.com/a/2468483) [tag:php]
- - [How to convert preg_replace e to preg_replace_callback?](http://stackoverflow.com/q/16367404)
- - [What does the `g` modifier mean?](http://stackoverflow.com/a/9622110)
- - [What does the `i` modifier mean?](http://stackoverflow.com/a/12411066)
- - [What does the `m` modifier mean?](http://stackoverflow.com/a/22438123) [tag:php] [tag:perl] [tag:python] [tag:javascript] [tag:.net] [tag:java]
- - [What does the `m` modifier mean in Ruby?](http://stackoverflow.com/a/4257912) [tag:ruby]
- - [What does the `o` modifier mean?](http://stackoverflow.com/a/13334823) [tag:ruby]
- - [What does the `s` modifier mean?](http://stackoverflow.com/a/13594017) (not supported by [tag:javascript], [tag:ruby])
- - [A workaround for JavaScript](http://stackoverflow.com/a/1068308)
- - [What does the `S` modifier mean?](http://stackoverflow.com/a/210027) [tag:php]
- - [What does the `u` modifier mean?](http://stackoverflow.com/a/2553239)
- - [What does the `U` modifier mean?](http://stackoverflow.com/a/5978385)
- - [What does the `x` modifier mean?](http://stackoverflow.com/a/2710390)
- - [What are inline modifiers?](http://stackoverflow.com/a/43636)
- [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
- to table of contents
- <H2>The Stack Overflow Regular Expressions FAQ</H2>
- ---
- <H3>Common Validation Tasks and Advanced Regex-Fu</H3>
- Common Validation Tasks:
- - Internet: [email addresses](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address), [urls](http://stackoverflow.com/a/190405/2736496) ([host/port](http://stackoverflow.com/a/22697740/578411)), [passwords](http://stackoverflow.com/a/3802238/2736496)
- - Numeric: [a number](http://stackoverflow.com/a/4247184/578411), [numeric min-max ranges (such as 1-31)](http://stackoverflow.com/a/22131040/2736496), [phone numbers](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation), [date](http://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy)
- - *Also see "When to *not* use regular expressions", under "General Information"
- Advanced Regex-Fu:
- - Strings and numbers:
- - [match a string not containing a word](http://stackoverflow.com/q/406230)
- - [How does this PCRE pattern detect palindromes?](http://stackoverflow.com/q/3746487)
- - [Match strings whose length is a fourth power](http://codegolf.stackexchange.com/q/19262)
- - [Get a string between two curly braces: `{...}`](http://stackoverflow.com/questions/413071/regex-to-get-string-between-curly-braces-i-want-whats-between-the-curly-brace)
- - [How does this regex find triangular numbers?](http://stackoverflow.com/q/3627681)
- - [How to determine if a number is a prime with regex?](http://stackoverflow.com/q/2795065)
- - Other:
- - [How can we match a^n b^n with Java regex?](http://stackoverflow.com/q/3644266)
- - Match nested brackets
- - [Using a recursive pattern](http://stackoverflow.com/a/17845034) [tag:php], [tag:perl]
- - [Using balancing groups](http://stackoverflow.com/a/17004406) [tag:.net]
- - [βverticalβ regex matching in an ASCII βimageβ](http://stackoverflow.com/q/17039670)
- - [Verbs that act after backtracking and failure](http://stackoverflow.com/q/19992984)
- - [The most up-voted regex questions on Code Golf](http://codegolf.stackexchange.com/questions/tagged/regular-expression?sort=votes&pageSize=50)
- [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
- to table of contents
- <H2>The Stack Overflow Regular Expressions FAQ</H2>
- ---
- <H3>Flavor-Specific Information</H3>
- Java:
- - Official docs: [Pattern JavaDoc](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html), [Sun's Regular expressions tutorial](http://docs.oracle.com/javase/tutorial/essential/regex/index.html)
- - The differences between functions in [`java.util.regex.Matcher`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html):
- - [`matches()`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#matches()): The match must be anchored to both input-start and -end
- - [`find()`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#find()): A match may be anywhere in the input string (substrings)
- - [`lookingAt()`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html): The match must be anchored to input-start only
- - *(For anchors in general, see the section "Anchors")*
- - The only [`java.lang.String`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) functions that accept regular expressions: [`matches(s)`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#matches%28java.lang.String%29), [`replaceAll(s,s)`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29), [`replaceFirst(s,s)`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceFirst%28java.lang.String,%20java.lang.String%29), [`split(s)`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String%29), [`split(s,i)`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29)
- - *[An (opinionated and) detailed discussion of the disadvantages of and missing features in `java.util.regex`](http://stackoverflow.com/a/5771326/2736496)
- *Non-stackoverflow links, except for those marked with `*`*
- Official documentation:
- - JavaScript 1.5 [general info](https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions) and [RegExp object](https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp)
- - [.NET](http://msdn.microsoft.com/en-us/library/hs600312(v=vs.110).aspx)
- - Databases: [Oracle](http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm), [MySQL](https://dev.mysql.com/doc/refman/5.1/en/regexp.html)
- - [Perl5 version 18.2](http://perldoc.perl.org/perlre.html)
- - Splunk: [regex terminology and syntax](http://docs.splunk.com/Documentation/Splunk/6.0.2/Knowledge/AboutSplunkregularexpressions#Terminology_and_syntax) and [regex command](http://docs.splunk.com/Documentation/Splunk/6.0.2/SearchReference/Regex)
- - Tcl: [manpage](http://www.tcl.tk/man/tcl8.4/TclCmd/regexp.htm), [`regexp` command](http://wiki.tcl.tk/986)
- [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
- to table of contents
- <H2>The Stack Overflow Regular Expressions FAQ</H2>
- ---
- <H3>General information</H3>
- - Other general documentation resources: *[Learning Regular Expressions](http://stackoverflow.com/questions/4736/learning-regular-expressions), [Regular-expressions.info](http://www.regular-expressions.info), [Wikipedia entry](http://en.wikipedia.org/wiki/Regular_expression), [Open-Directory Project](http://www.dmoz.org/Computers/Programming/Languages/Regular_Expressions), [Rex Egg](http://www.rexegg.com/)
- - *[DFA versus NFA](http://stackoverflow.com/questions/3978438/dfa-vs-nfa-engines-what-is-the-difference-in-their-capabilities-and-limitations)
- - Books: Jeffrey Friedl's [Mastering Regular Expressions](http://regex.info/book.html)
- - When to *not* use regular expressions:
- - [Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.](http://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/) *(blog written by stackoverflow's founder)*
- - *[Do not use regular expressions to parse HTML](http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not). *[*It's for your own safety!*](http://stackoverflow.com/a/1732454/2736496))
- *Non-stackoverflow links, except for those marked with `*`*
- [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
- to table of contents
- <H2>The Stack Overflow Regular Expressions FAQ</H2>
- ---
- <H3>Tools: Testers and Explainers</H3>
- Online (*=Also has a replacement tester):
- - [Debuggex](http://debuggex.com) [tag:javascript], [tag:python], [tag:pcre]
- - *[Regular Expressions 101](http://regex101.com) [tag:php] PCRE, [tag:python], [tag:javascript]
- - [Regex Pal](http://regexpal.com/), *[regular-expressions.info](http://www.regular-expressions.info/javascriptexample.html) [tag:javascript]
- - [Rublar](http://rubular.com/) [tag:ruby]
- - [RegExr](http://www.regexr.com/)
- - *RegexPlanet: [Java](http://www.regexplanet.com/advanced/java/index.html) [tag:java], [Go](http://www.regexplanet.com/advanced/golang/index.html) [tag:go], [Haskell](http://www.regexplanet.com/advanced/haskell/index.html) [tag:haskell], [JavaScript](http://www.regexplanet.com/advanced/javascript/index.html) [tag:javascript], [.NET](http://www.regexplanet.com/advanced/dotnet/index.html) [tag:dotnet], [Perl](http://www.regexplanet.com/advanced/perl/index.html) [tag:perl], [PHP PCRE](http://www.regexplanet.com/advanced/php/index.html) [tag:php], [Python](http://www.regexplanet.com/advanced/python/index.html) [tag:python], [Ruby](http://www.regexplanet.com/advanced/ruby/index.html) [tag:ruby], [XRegExp](http://www.regexplanet.com/advanced/xregexp/index.html) [tag:xregexp]
- - [`freeformatter.com`](http://www.freeformatter.com/regex-tester.html) [tag:xregexp]
- - *[`regex.larsolavtorvik.com`](http://regex.larsolavtorvik.com/) [tag:php] PCRE and POSIX, [tag:javascript]
- - [Regex Hero](http://regexhero.net/tester) [tag:dotnet]
- Offline
- - Microsoft Windows: [RegexBuddy](http://regexbuddy.com) (analysis), [RegexMagic](http://regexmagic.com) (creation)
- *(This section contains non-stackoverflow links)*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement