This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 11th, 2014  |  syntax: None  |  size: 17.83 KB  |  views: 79  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <h2>The Stack Overflow Regular Expressions FAQ</h2>
  2.  
  3. ---
  4.  
  5. <H3>Table of contents</H3>
  6.  
  7. Stack Overflow questions and answers:
  8.  
  9.  - [Quantifiers](URL): Greedy, possessive, reluctant, `*`:0-or-more, `+`:1-or-more, `{min,max}`
  10.  - [Character Classes](URL)
  11.  - [Special Characters, Anchors, Other](URL)
  12.  - [Groups](URL)
  13.  - [Lookarounds](URL)
  14.  - [Modifiers](URL)
  15.  - [Common Validation Tasks and Advanced Regex-Fu](URL)
  16.  
  17. External links:
  18.  
  19.  - [Flavor-Specific Information](URL)
  20.  - [General information](URL)
  21.  - [Tools: Testers and Explainers](URL): Websites and applications
  22.  
  23. ---
  24.  
  25. [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
  26.  to table of contents
  27.  
  28. <H2>The Stack Overflow Regular Expressions FAQ</H2>
  29.  
  30. ---
  31.  
  32. <H3>Quantifiers</H3>
  33.  
  34.  - Zero-or-more: [`*`:greedy](http://stackoverflow.com/a/10764399), [`*?`:reluctant](http://stackoverflow.com/a/10764399), [`*+`:possessive](http://stackoverflow.com/a/17064242)
  35.  - One-or-more: [`+`:greedy](http://stackoverflow.com/a/10764399), [`+?`:reluctant](http://stackoverflow.com/a/10764399), [`++`:possessive](http://stackoverflow.com/q/4489551)
  36.  - [`?`:optional (zero-or-one)](http://stackoverflow.com/a/17400486)
  37.  - 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)
  38.  - More on the differences between greedy, possessive, and reluctant:
  39.    - (Greedy vs non-greedy, non-greedy also known as "lazy", "ungreedy" or "reluctant")
  40.    - [Greedy vs. Reluctant vs. Possessive Quantifiers](http://stackoverflow.com/q/5319840)
  41.    - [In-depth discussion on the differences between greedy versus non-greedy](http://stackoverflow.com/a/3075532)
  42.    - [What's the difference between `{n}` and `{n}?`](http://stackoverflow.com/q/18006093)
  43.    - [Can someone explain Possessive Quantifiers to me?](http://stackoverflow.com/q/1117467) [tag:php], [tag:perl], [tag:java], [tag:ruby]
  44.    - [Emulating possessive quantifiers](http://stackoverflow.com/q/5537513) [tag:.net]
  45.    - 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)
  46.  
  47. [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
  48.  to table of contents
  49.  
  50. <H2>The Stack Overflow Regular Expressions FAQ</H2>
  51.  
  52. ---
  53.  
  54. <H3>Character Classes</H3>
  55.  
  56.  - [`[...]`](http://stackoverflow.com/a/1553171):any one character, [`[^...]`](http://stackoverflow.com/a/20802463):negated/any character but
  57.  - [`[...-...]` / `[range-[bar]]`](http://stackoverflow.com/a/10738860):any one character in range [tag:java], [tag:ruby] 1.9+
  58.  - [`[range&&[bar]]`:intersection](http://stackoverflow.com/a/15935168) [tag:java], [tag:ruby] 1.9+
  59.  - [`[[:alpha:]]`](http://stackoverflow.com/a/12276342):POSIX character classes
  60.  - [Why do `[^\\D2]`, `[^[^0-9]2]`, `[^2[^0-9]]` get different results in Java?](http://stackoverflow.com/q/21934168) [tag:java]
  61.  - Short-Hand:
  62.     - Digit: [`\d`:digit](http://stackoverflow.com/a/16621778), [`\D`:non-digit](http://stackoverflow.com/a/19011185)
  63.     - Word character (Letter, digit, underscore): [`\w`:word character](http://stackoverflow.com/a/11874899), [`\W`:non-word character](http://stackoverflow.com/a/19011185)
  64.     - Whitespace: [`\s`:whitespace](http://stackoverflow.com/a/21067350), [`\S`:non-whitespace](http://stackoverflow.com/a/19011185)
  65.  
  66. [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
  67.  to table of contents
  68.  
  69. <H2>The Stack Overflow Regular Expressions FAQ</H2>
  70.  
  71. ---
  72.  
  73. <H3>Special Characters, Anchors, Other</H3>
  74.  
  75. Special characters:
  76.  
  77.  - Horizontal whitespace: [`\h`:space-or-tab](http://stackoverflow.com/a/4910093), [`\t`:tab](http://stackoverflow.com/a/17950891)
  78.  - Newlines:
  79.    - [`\r`, `\n`:carriage return and line feed](http://stackoverflow.com/a/3451192)
  80.    - [`\R`:generic newline](http://stackoverflow.com/a/18992691) [tag:php]
  81.  - Other: [`\v`:vertical tab](http://stackoverflow.com/q/12290224), [`\e`:the escape character](http://stackoverflow.com/a/4275788)
  82.  
  83. Anchors:
  84.  
  85.  - *(Also see "Flavor-Specific Information > Java > The functions in `Matcher`")*
  86.  - [`^`: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)
  87.  - [`\A`:start of input, `\Z`:end of input](http://stackoverflow.com/a/4020821) [tag:php], [tag:perl], [tag:python], [tag:ruby]
  88.  - [`\G`:start of match](http://stackoverflow.com/q/21971701) [tag:php], [tag:perl], [tag:ruby]
  89.  
  90. Other:
  91.  
  92.  - [`|`:or operator](http://stackoverflow.com/a/22187948)
  93.  - [`.`:any character](http://stackoverflow.com/a/13594017), [`[.]`:literal dot character](http://stackoverflow.com/a/21929764)
  94.  - [What special characters must be escaped?](http://stackoverflow.com/q/399078)
  95.  - 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)
  96.    - [tag:php] only: [`(*BSR_ANYCRLF)`](http://stackoverflow.com/a/7374702)
  97.  - 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)
  98.  
  99. [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
  100.  to table of contents
  101.  
  102. <H2>The Stack Overflow Regular Expressions FAQ</H2>
  103.  
  104. ---
  105.  
  106. <H3>Groups</H3>
  107.  
  108.  - [`(...)`:capture group](http://stackoverflow.com/q/21880127), [`(?:)`:non-capture group](http://stackoverflow.com/q/3512471)
  109.  - [`\1`:backreference and capture-group reference, `$1`:capture group reference](http://stackoverflow.com/q/21880127)
  110.    - [`\g<1>123`:How to follow a numbered capture group, such as `\1`, with a number?:](http://stackoverflow.com/q/5984633) [tag:python]
  111.  - [What does a subpattern `(?i:regex)` mean?](http://stackoverflow.com/a/3812728)
  112.  - [`(?>)`:atomic group](http://stackoverflow.com/q/14411818), [`(?|)`:branch reset](http://stackoverflow.com/a/5333645)
  113.    - [Equivalent of branch reset in .NET/C#](http://stackoverflow.com/a/5378077) [tag:.net]
  114.  - Named capture groups:
  115.     - [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)*
  116.     - 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]
  117.  
  118.  
  119. [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
  120.  to table of contents
  121.  
  122. <H2>The Stack Overflow Regular Expressions FAQ</H2>
  123.  
  124. ---
  125.  
  126. <H3>Lookarounds</H3>
  127.  
  128.  - Lookaheads: [`(?=...)`:positive](http://stackoverflow.com/a/1570916), [`(?!...)`:negative](http://stackoverflow.com/a/12210820)
  129.  - Lookbehinds: [`(?<=...)`:positive](http://stackoverflow.com/a/11197672), [`(?<!...)`:negative](http://stackoverflow.com/a/11197672) (not supported by [tag:javascript])
  130.  - Lookbehind limits in:
  131.    - [Lookbehinds need to be constant-length](http://stackoverflow.com/a/22821726) [tag:php], [tag:perl], [tag:python], [tag:ruby]
  132.    - [Lookarounds of limited length `{0,n}`](http://stackoverflow.com/a/20994257) [tag:java]
  133.    - [Variable length lookbehinds are allowed](http://stackoverflow.com/a/20994257) [tag:.net]
  134.  - Lookbehind alternatives:
  135.    - [Using `\K`](http://stackoverflow.com/a/11640500) [tag:php], [tag:perl]
  136.      - [Support of `\K` in regex](http://stackoverflow.com/a/13543042)
  137.    - [Alternative regex module for python](http://stackoverflow.com/a/11641102) [tag:python]
  138.      - [The hacky way](http://stackoverflow.com/a/11640862)
  139.      - [Mimicking Lookbehind in JavaScript](http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript) <sup>External link, someone got one from SO?</sup>
  140.  
  141. [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
  142.  to table of contents
  143.  
  144. <H2>The Stack Overflow Regular Expressions FAQ</H2>
  145.  
  146. ---
  147.  
  148. <H3>Modifiers</H3>
  149.  
  150.  - [What does the `c` modifier mean?](http://stackoverflow.com/a/11395687) [tag:perl]
  151.  - [What does the `e` modifier mean?](http://stackoverflow.com/a/2468483) [tag:php]
  152.    - [How to convert preg_replace e to preg_replace_callback?](http://stackoverflow.com/q/16367404)
  153.  - [What does the `g` modifier mean?](http://stackoverflow.com/a/9622110)
  154.  - [What does the `i` modifier mean?](http://stackoverflow.com/a/12411066)
  155.  - [What does the `m` modifier mean?](http://stackoverflow.com/a/22438123) [tag:php] [tag:perl] [tag:python] [tag:javascript] [tag:.net] [tag:java]
  156.    - [What does the `m` modifier mean in Ruby?](http://stackoverflow.com/a/4257912) [tag:ruby]
  157.  - [What does the `o` modifier mean?](http://stackoverflow.com/a/13334823) [tag:ruby]
  158.  - [What does the `s` modifier mean?](http://stackoverflow.com/a/13594017) (not supported by [tag:javascript], [tag:ruby])
  159.    - [A workaround for JavaScript](http://stackoverflow.com/a/1068308)
  160.  - [What does the `S` modifier mean?](http://stackoverflow.com/a/210027) [tag:php]
  161.  - [What does the `u` modifier mean?](http://stackoverflow.com/a/2553239)
  162.  - [What does the `U` modifier mean?](http://stackoverflow.com/a/5978385)
  163.  - [What does the `x` modifier mean?](http://stackoverflow.com/a/2710390)
  164.  - [What are inline modifiers?](http://stackoverflow.com/a/43636)
  165.  
  166. [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
  167.  to table of contents
  168.  
  169. <H2>The Stack Overflow Regular Expressions FAQ</H2>
  170.  
  171. ---
  172.  
  173. <H3>Common Validation Tasks and Advanced Regex-Fu</H3>
  174.  
  175. Common Validation Tasks:
  176.  
  177.  - 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)
  178.  - 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)
  179.  - *Also see "When to *not* use regular expressions", under "General Information"
  180.  
  181. Advanced Regex-Fu:
  182.  
  183.  - Strings and numbers:
  184.    - [match a string not containing a word](http://stackoverflow.com/q/406230)
  185.    - [How does this PCRE pattern detect palindromes?](http://stackoverflow.com/q/3746487)
  186.    - [Match strings whose length is a fourth power](http://codegolf.stackexchange.com/q/19262)
  187.    - [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)
  188.    - [How does this regex find triangular numbers?](http://stackoverflow.com/q/3627681)
  189.    - [How to determine if a number is a prime with regex?](http://stackoverflow.com/q/2795065)
  190.  - Other:
  191.    - [How can we match a^n b^n with Java regex?](http://stackoverflow.com/q/3644266)
  192.    - Match nested brackets
  193.      - [Using a recursive pattern](http://stackoverflow.com/a/17845034) [tag:php], [tag:perl]
  194.      - [Using balancing groups](http://stackoverflow.com/a/17004406) [tag:.net]
  195.    - [“vertical” regex matching in an ASCII “image”](http://stackoverflow.com/q/17039670)
  196.    - [Verbs that act after backtracking and failure](http://stackoverflow.com/q/19992984)
  197.    - [The most up-voted regex questions on Code Golf](http://codegolf.stackexchange.com/questions/tagged/regular-expression?sort=votes&pageSize=50)
  198.  
  199. [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
  200.  to table of contents
  201.  
  202. <H2>The Stack Overflow Regular Expressions FAQ</H2>
  203.  
  204. ---
  205.  
  206. <H3>Flavor-Specific Information</H3>
  207.  
  208. Java:
  209.  
  210.  - 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)
  211.  - The differences between functions in [`java.util.regex.Matcher`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html):
  212.     - [`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
  213.     - [`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)
  214.     - [`lookingAt()`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html): The match must be anchored to input-start only
  215.     - *(For anchors in general, see the section "Anchors")*
  216.  - 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)
  217.  - *[An (opinionated and) detailed discussion of the disadvantages of and missing features in `java.util.regex`](http://stackoverflow.com/a/5771326/2736496)
  218.  
  219. *Non-stackoverflow links, except for those marked with `*`*
  220.  
  221. Official documentation:
  222.  
  223.  - 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)
  224.  - [.NET](http://msdn.microsoft.com/en-us/library/hs600312(v=vs.110).aspx)
  225.  - 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)
  226.  - [Perl5 version 18.2](http://perldoc.perl.org/perlre.html)
  227.  - 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)
  228.  - Tcl: [manpage](http://www.tcl.tk/man/tcl8.4/TclCmd/regexp.htm), [`regexp` command](http://wiki.tcl.tk/986)
  229.  
  230. [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
  231.  to table of contents
  232.  
  233. <H2>The Stack Overflow Regular Expressions FAQ</H2>
  234.  
  235. ---
  236.  
  237. <H3>General information</H3>
  238.  
  239.  - 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/)
  240.  - *[DFA versus NFA](http://stackoverflow.com/questions/3978438/dfa-vs-nfa-engines-what-is-the-difference-in-their-capabilities-and-limitations)
  241.  - Books: Jeffrey Friedl's [Mastering Regular Expressions](http://regex.info/book.html)
  242.  - When to *not* use regular expressions:
  243.    - [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)*
  244.    - *[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))
  245.  
  246. *Non-stackoverflow links, except for those marked with `*`*
  247.  
  248.  
  249. [`<-BACK`](http://stackoverflow.com/a/22944075/2736496)
  250.  to table of contents
  251.  
  252. <H2>The Stack Overflow Regular Expressions FAQ</H2>
  253.  
  254. ---
  255.  
  256. <H3>Tools: Testers and Explainers</H3>
  257.  
  258. Online (*=Also has a replacement tester):
  259.  
  260.  - [Debuggex](http://debuggex.com) [tag:javascript], [tag:python], [tag:pcre]
  261.  - *[Regular Expressions 101](http://regex101.com) [tag:php] PCRE, [tag:python], [tag:javascript]
  262.  - [Regex Pal](http://regexpal.com/), *[regular-expressions.info](http://www.regular-expressions.info/javascriptexample.html) [tag:javascript]
  263.  - [Rublar](http://rubular.com/) [tag:ruby]
  264.  - [RegExr](http://www.regexr.com/)
  265.  - *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]
  266.  - [`freeformatter.com`](http://www.freeformatter.com/regex-tester.html) [tag:xregexp]
  267.  - *[`regex.larsolavtorvik.com`](http://regex.larsolavtorvik.com/) [tag:php] PCRE and POSIX, [tag:javascript]
  268.  - [Regex Hero](http://regexhero.net/tester) [tag:dotnet]
  269.  
  270. Offline
  271.  
  272.  - Microsoft Windows: [RegexBuddy](http://regexbuddy.com) (analysis), [RegexMagic](http://regexmagic.com) (creation)
  273.  
  274. *(This section contains non-stackoverflow links)*
clone this paste RAW Paste Data