Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Regular expression
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Possessive matching=== In Java and Python 3.11+,<ref>[https://github.com/python/cpython/issues/34627/ SRE: Atomic Grouping (?>...) is not supported #34627]</ref> quantifiers may be made ''possessive'' by appending a plus sign, which disables backing off (in a backtracking engine), even if doing so would allow the overall match to succeed:<ref name=es-re>{{cite web |title=Essential classes: Regular Expressions: Quantifiers: Differences Among Greedy, Reluctant, and Possessive Quantifiers |url=https://docs.oracle.com/javase/tutorial/essential/regex/quant.html#difs |website=The Java Tutorials |publisher=[[Oracle Corporation|Oracle]] |access-date=23 December 2016 |archive-date=7 October 2020 |archive-url=https://web.archive.org/web/20201007183203/https://docs.oracle.com/javase/tutorial/essential/regex/quant.html#difs |url-status=live}}</ref> While the regex <code>".*"</code> applied to the string "Ganymede," he continued, "is the largest moon in the Solar System." matches the entire line, the regex <code>".*+"</code> does {{em|not match at all}}, because <code>.*+</code> consumes the entire input, including the final <code>"</code>. Thus, possessive quantifiers are most useful with negated character classes, e.g. <code>"[^"]*+"</code>, which matches <code>"Ganymede,"</code> when applied to the same string. Another common extension serving the same function is atomic grouping, which disables backtracking for a parenthesized group. The typical syntax is {{mono|1=(?>group)}}. For example, while {{mono|1=^(wi{{!}}w)i$}} matches both {{mono|wi}} and {{mono|wii}}, {{mono|1=^(?>wi{{!}}w)i$}} only matches {{mono|wii}} because the engine is forbidden from backtracking and so cannot try setting the group to "w" after matching "wi".<ref>{{cite web |title=Atomic Grouping |url=https://www.regular-expressions.info/atomic.html |website=Regex Tutorial |access-date=24 November 2019 |archive-date=7 October 2020 |archive-url=https://web.archive.org/web/20201007183204/https://www.regular-expressions.info/atomic.html |url-status=live}}</ref> Possessive quantifiers are easier to implement than greedy and lazy quantifiers, and are typically more efficient at runtime.<ref name=es-re/>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)