Участник:Vavilexxx/Статья хаоса 12

Материал из Википедии — свободной энциклопедии

 МЕНЮ
Последняя правка: пятница, 17:59, 28 июня 2024 года участником: Vavilexxx

RegExp

Pattern Description
[abc] A single character of: a, b or c
[^abc] A character except: a, b or c
[a-z] A character in the range: a-z
[^a-z] A character not in the range: a-z
[0-9] A digit in the range: 0-9
[a-zA-Z] A character in the range:
a-z or A-Z
[a-zA-Z0-9] A character in the range:
a-z, A-Z or 0-9

Quantifiers

Pattern Description
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
[0-9]+ One or more of 0-9
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a
a* Greedy quantifier
a*? Lazy quantifier
a*+ Possessive quantifier

Common Metacharacters

  • ^
  • {
  • +
  • <
  • [
  • *
  • )
  • >
  • .
  • (
  • |
  • $
  • \
  • ?

Escape these special characters with \

Meta Sequences

Pattern Description
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit, Same as [0-9]
\D Any non-digit, Same as [^0-9]
\w Any word character
\W Any non-word character
\X Any Unicode sequences, linebreaks included
\C Match one data unit
\R Unicode newlines
\v Vertical whitespace character
\V Negation of \v — anything except newlines and vertical tabs
\h Horizontal whitespace character
\H Negation of \h
\K Reset match
\n Match nth subpattern
\pX Unicode property X
\p{...} Unicode property or script category
\PX Negation of \pX
\P{...} Negation of \p
\Q...\E Quote; treat as literals
\k<name> Match subpattern name
\k'name' Match subpattern name
\k{name} Match subpattern name
\gn Match nth subpattern
\g{n} Match nth subpattern
\g<n> Recurse nth capture group
\g'n' Recurses nth capture group.
\g{-n} Match nth relative previous subpattern
\g<+n> Recurse nth relative upcoming subpattern
\g'+n' Match nth relative upcoming subpattern
\g'letter' Recurse named capture group letter
\g{letter} Match previously-named capture group letter
\g<letter> Recurses named capture group letter
\xYY Hex character YY
\x{YYYY} Hex character YYYY
\ddd Octal character ddd
\cY Control character Y
[\b] Backspace character
\ Makes any character literal

Anchors

Pattern Description
\G Start of match
^ Start of string
$ End of string
\A Start of string
\Z End of string
\z Absolute end of string
\b A word boundary
\B Non-word boundary

Substitution

Pattern Description
\0 Complete match contents
\1 Contents in capture group 1
$1 Contents in capture group 1
${foo} Contents in capture group foo
\x20 Hexadecimal replacement values
\x{06fa} Hexadecimal replacement values
\t Tab
\r Carriage return
\n Newline
\f Form-feed
\U Uppercase Transformation
\L Lowercase Transformation
\E Terminate any Transformation

Group Constructs

Pattern Description
(...) Capture everything enclosed
(a|b) Match either a or b
(?:...) Match everything enclosed
(?>...) Atomic group (non-capturing)
(?|...) Duplicate subpattern group number
(?#...) Comment
(?'name'...) Named Capturing Group
(?<name>...) Named Capturing Group
(?P<name>...) Named Capturing Group
(?imsxXU) Inline modifiers
(?(DEFINE)...) Pre-define patterns before using them

Assertions

- -
(?(1)yes|no) Conditional statement
(?(R)yes|no) Conditional statement
(?(R#)yes|no) Recursive Conditional statement
(?(R&name)yes|no) Conditional statement
(?(?=...)yes|no) Lookahead conditional
(?(?<=...)yes|no) Lookbehind conditional

Lookarounds

- -
(?=...) Positive Lookahead
(?!...) Negative Lookahead
(?<=...) Positive Lookbehind
(?<!...) Negative Lookbehind

Lookaround lets you match a group before (lookbehind) or after (lookahead) your main pattern without including it in the result.

Flags/Modifiers

Pattern Description
g Global
m Multiline
i Case insensitive
x Ignore whitespace
s Single line
u Unicode
X eXtended
U Ungreedy
A Anchor
J Duplicate group names

Recurse

- -
(?R) Recurse entire pattern
(?1) Recurse first subpattern
(?+1) Recurse first relative subpattern
(?&name) Recurse subpattern name
(?P=name) Match subpattern name
(?P>name) Recurse subpattern name

POSIX Character Classes

Character Class Same as Meaning
alnum: [0-9A-Za-z] Letters and digits
alpha: [A-Za-z] Letters
ascii: [\x00-\x7F] ASCII codes 0-127
blank: [\t ] Space or tab only
cntrl: [\x00-\x1F\x7F] Control characters
digit: [0-9] Decimal digits
graph: [[:alnum:][:punct:]] Visible characters (not space)
lower: [a-z] Lowercase letters
print: [ -~] == [ [:graph:]] Visible characters
punct: [!"#$%&’()*+,-./:;<=>?@[]^_`{|}~] Visible punctuation characters
space: [\t\n\v\f\r ] Whitespace
upper: [A-Z] Uppercase letters
word: [0-9A-Za-z_] Word characters
xdigit: [0-9A-Fa-f] Hexadecimal digits
[[:<:]] [\b(?=\w)] Start of word
[[:>:]] [\b(?<=\w)] End of word

Control verb

- -
(*ACCEPT) Control verb
(*FAIL) Control verb
(*MARK:NAME) Control verb
(*COMMIT) Control verb
(*PRUNE) Control verb
(*SKIP) Control verb
(*THEN) Control verb
(*UTF) Pattern modifier
(*UTF8) Pattern modifier
(*UTF16) Pattern modifier
(*UTF32) Pattern modifier
(*UCP) Pattern modifier
(*CR) Line break modifier
(*LF) Line break modifier
(*CRLF) Line break modifier
(*ANYCRLF) Line break modifier
(*ANY) Line break modifier
\R Line break modifier
(*BSR_ANYCRLF) Line break modifier
(*BSR_UNICODE) Line break modifier
(*LIMIT_MATCH=x) Regex engine modifier
(*LIMIT_RECURSION=d) Regex engine modifier
(*NO_AUTO_POSSESS) Regex engine modifier
(*NO_START_OPT) Regex engine modifier

Regex examples

Characters

Pattern Matches
ring Match
                       <yel>ring</yel>
                       sp
                       <yel>ring</yel>
                       board etc.
. Match
                           <yel>a</yel>
                           ,
                           <yel>9</yel>
                           ,
                           <yel>+</yel>
                           etc.
h.o Match
                           <yel>hoo</yel>
                           ,
                           <yel>h2o</yel>
                           ,
                           <yel>h/o</yel>
                           etc.
ring\? Match
                           <yel>ring?</yel>
\(quiet\) Match
                           <yel>(quiet)</yel>
c:\\windows Match
                           <yel>c:\windows</yel>

Use \ to search for these special characters:
[ \ ^ $ . | ? * + ( ) { }

Alternatives

Pattern Matches
cat|dog Match
                       <yel>cat</yel>
                       or
                       <yel>dog</yel>
id|identity Match
                           <yel>id</yel>
                           or
                           <yel>id</yel>
                           entity
identity|id Match
                           <yel>id</yel>
                           or
                           <yel>identity</yel>

Order longer to shorter when alternatives overlap

Character classes

Pattern Matches
[aeiou] Match any vowel
[^aeiou] Match a NON vowel
r[iau]ng Match
                           <yel>ring</yel>
                           , w
                           <yel>rang</yel>
                           le, sp
                           <yel>rung</yel>
                           , etc.
gr[ae]y Match
                           <yel>gray</yel>
                           or
                           <yel>grey</yel>
[a-zA-Z0-9] Match any letter or digit
[\u3a00-\ufa99] Match any

In [ ] always escape . \ ] and sometimes ^ - .

Shorthand classes

Pattern Meaning
\w В«WordВ» character
(letter, digit, or underscore)
\d Digit
\s Whitespace
(space, tab, vtab, newline)
\W, \D, or \S Not word, digit, or whitespace
[\D\S] Means not digit or whitespace, both match
[^\d\s] Disallow digit and whitespace

Occurrences

Pattern Matches
colou?r Match
                       <yel>color</yel>
                       or
                       <yel>colour</yel>
[BW]ill[ieamy's]* Match
                           <yel>Bill</yel>
                           ,
                           <yel>Willy</yel>
                           ,
                           <yel>William’s</yel>
                           etc.
[a-zA-Z]+ Match 1 or more letters
\d{3}-\d{2}-\d{4} Match a SSN
[a-z]\w{1,7} Match a UW NetID

Greedy versus lazy

Pattern Meaning
* + {n,}
greedy
Match as much as possible
<.+> Finds 1 big match in
                           <yel><b>bold</b></yel>
*? +? {n,}?
lazy
Match as little as possible
<.+?> Finds 2 matches in <<yel>b</yel>>bold<<yel>/b</yel>>

Scope

Pattern Meaning
\b В«WordВ» edge (next to non В«wordВ» character)
\bring Word starts with В«ringВ», ex
                           <yel>ringtone</yel>
ring\b Word ends with В«ringВ», ex
                           <yel>spring</yel>
\b9\b Match single digit
                           <yel>9</yel>
                           , not 19, 91, 99, etc..
\b[a-zA-Z]{6}\b Match 6-letter words
\B Not word edge
\Bring\B Match
                           <yel>springs</yel>
                           and
                           <yel>wringer</yel>
^\d*$ Entire string must be digits
^[a-zA-Z]{4,20}$ String must have 4-20 letters
^[A-Z] String must begin with capital letter
[\.!?"')]$ String must end with terminal puncutation

Modifiers

Pattern Meaning
(?i)[a-z]*(?-i) Ignore case ON / OFF
(?s).*(?-s) Match multiple lines (causes . to match newline)
(?m)^.*;$(?-m)
                           <yel>^</yel> &
                           <yel>$</yel>
                           match lines not whole string
(?x) #free-spacing mode, this EOL comment ignored
(?-x) free-spacing mode OFF
/regex/ismx Modify mode for entire string

Groups

Pattern Meaning
(in\|out)put Match
                       <yel>input</yel>
                       or
                       <yel>output</yel>
\d{5}(-\d{4})? US zip code (В«+ 4В» optional)

Parser tries EACH alternative if match fails after group.
Can lead to catastrophic backtracking.

Back references

Pattern Matches
(to) (be) or not \1 \2 Match
                       <yel>to be or not to be</yel>
([^\s])\1{2} Match non-space, then same twice more В 
                           <yel>aaa</yel>
                           ,
                           <yel>…</yel>
\b(\w+)\s+\1\b Match doubled words

Non-capturing group

Pattern Meaning
on(?:click\|load) Faster than:
on(click\|load)

Use non-capturing or atomic groups when possible

Atomic groups

Pattern Meaning
(?>red\|green\|blue) Faster than non-capturing
(?>id\|identity)\b Match
                           <yel>id</yel>
                           , but not
                           <yel>id</yel>
                           entity

«id» matches, but \b fails after atomic group, parser doesn’t backtrack into group to retry 'identity'

If alternatives overlap, order longer to shorter.

Lookaround

Pattern Meaning
(?= ) Lookahead, if you can find ahead
(?! ) Lookahead, if you can not find ahead
(?<= ) Lookbehind, if you can find behind
(?<! ) Lookbehind, if you can NOT find behind
\b\w+?(?=ing\b) Match
                           <yel>warbl</yel>
                           ing,
                           <yel>str</yel>
                           ing,
                           <yel>fish</yel>
                           ing, …
\b(?!\w+ing\b)\w+\b Words NOT ending in В«ingВ»
(?<=\bpre).*?\b Match pre
                           <yel>tend</yel>
                           , pre
                           <yel>sent</yel>
                           , pre
                           <yel>fix</yel>
                           , …
\b\w{3}(?<!pre)\w*?\b Words NOT starting with В«preВ»
\b\w+(?<!ing)\b Match words NOT ending in В«ingВ»

If-then-else

Match В«Mr.В» or В«Ms.В» if word В«herВ» is later in string

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code>M(<span>?(</span><span>?=</span>.*<span>?\b</span>her\b)s|<span>r)\.
</span></code>

requires lookaround for IF condition

RegEx in Python

Getting started

Import the regular expressions module

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>import</span> re
</code>

Examples

re.search()

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>>>> </span>sentence = <span>'This is a sample string'</span>
<span>>>> </span><span>bool</span>(re.search(<span>r'this'</span>, sentence, flags=re.I))
<span>True</span>
<span>>>> </span><span>bool</span>(re.search(<span>r'xyz'</span>, sentence))
<span>False</span>
</code>

re.findall()

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>>>> </span>re.findall(<span>r'\bs?pare?\b'</span>, <span>'par spar apparent spare part pare'</span>)
[<span>'par'</span>, <span>'spar'</span>, <span>'spare'</span>, <span>'pare'</span>]
<span>>>> </span>re.findall(<span>r'\b0*[1-9]\d{2,}\b'</span>, <span>'0501 035 154 12 26 98234'</span>)
[<span>'0501'</span>, <span>'154'</span>, <span>'98234'</span>]
</code>

re.finditer()

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>>>> </span>m_iter = re.finditer(<span>r'[0-9]+'</span>, <span>'45 349 651 593 4 204'</span>)
<span>>>> </span>[m[<span>0</span>] <span>for</span> m <span>in</span> m_iter <span>if</span> <span>int</span>(m[<span>0</span>]) < <span>350</span>]
[<span>'45'</span>, <span>'349'</span>, <span>'4'</span>, <span>'204'</span>]
</code>

re.split()

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>>>> </span>re.split(<span>r'\d+'</span>, <span>'Sample123string42with777numbers'</span>)
[<span>'Sample'</span>, <span>'string'</span>, <span>'with'</span>, <span>'numbers'</span>]
</code>

re.sub()

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>>>> </span>ip_lines = <span>"catapults\nconcatenate\ncat"</span>
<span>>>> </span><span>print</span>(re.sub(<span>r'^'</span>, <span>r'* '</span>, ip_lines, flags=re.M))
* catapults
* concatenate
* cat
</code>

re.compile()

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>>>> </span>pet = re.<span>compile</span>(<span>r'dog'</span>)
<span>>>> </span><span>type</span>(pet)
<<span>class</span> <span>'_sre.SRE_Pattern'</span>>
<span>>>> </span><span>bool</span>(pet.search(<span>'They bought a dog'</span>))
<span>True</span>
<span>>>> </span><span>bool</span>(pet.search(<span>'A cat crossed their path'</span>))
<span>False</span>
</code>

Functions

Function Description
re.findall Returns a list containing all matches
re.finditer Return an iterable of match objects (one for each match)
re.search Returns a Match object if there is a match anywhere in the string
re.split Returns a list where the string has been split at each match
re.sub Replaces one or many matches with a string
re.compile Compile a regular expression pattern for later use
re.escape Return string with all non-alphanumerics backslashed

Flags

- - -
re.I re.IGNORECASE Ignore case
re.M re.MULTILINE Multiline
re.L re.LOCALE Make \w,\b,\s locale dependent
re.S re.DOTALL Dot matches all (including newline)
re.U re.UNICODE Make \w,\b,\d,\s unicode dependent
re.X re.VERBOSE Readable style

Regex in JavaScript

test()

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>let</span> textA = <span>'I like APPles very much'</span>;
<span>let</span> textB = <span>'I like APPles'</span>;
<span>let</span> regex = <span>/apples$/i</span>

<span>// Output: false</span>
<span>console</span>.<span>log</span>(regex.<span>test</span>(textA));

<span>// Output: true</span>
<span>console</span>.<span>log</span>(regex.<span>test</span>(textB));
</code>

search()

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>let</span> text = <span>'I like APPles very much'</span>;
<span>let</span> regexA = <span>/apples/</span>;
<span>let</span> regexB = <span>/apples/i</span>;

<span>// Output: -1</span>
<span>console</span>.<span>log</span>(text.<span>search</span>(regexA));

<span>// Output: 7</span>
<span>console</span>.<span>log</span>(text.<span>search</span>(regexB));
</code>

exec()

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>let</span> text = <span>'Do you like apples?'</span>;
<span>let</span> regex= <span>/apples/</span>;

<span>// Output: apples</span>
<span>console</span>.<span>log</span>(regex.<span>exec</span>(text)[<span>0</span>]);

<span>// Output: Do you like apples?</span>
<span>console</span>.<span>log</span>(regex.<span>exec</span>(text).<span>input</span>);
</code>

match()

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>let</span> text = <span>'Here are apples and apPleS'</span>;
<span>let</span> regex = <span>/apples/gi</span>;

<span>// Output: [ "apples", "apPleS" ]</span>
<span>console</span>.<span>log</span>(text.<span>match</span>(regex));
</code>

split()

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>let</span> text = <span>'This 593 string will be brok294en at places where d1gits are.'</span>;
<span>let</span> regex = <span>/\d+/g</span>

<span>// Output: [ "This ", " string will be brok", "en at places where d", "gits are." ] </span>
<span>console</span>.<span>log</span>(text.<span>split</span>(regex))
</code>

matchAll()

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>let</span> regex = <span>/t(e)(st(\d?))/g</span>;
<span>let</span> text = <span>'test1test2'</span>;
<span>let</span> array = [...text.<span>matchAll</span>(regex)];

<span>// Output: ["test1", "e", "st1", "1"]</span>
<span>console</span>.<span>log</span>(array[<span>0</span>]);

<span>// Output: ["test2", "e", "st2", "2"]</span>
<span>console</span>.<span>log</span>(array[<span>1</span>]);
</code>

replace()

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>let</span> text = <span>'Do you like aPPles?'</span>;
<span>let</span> regex = <span>/apples/i</span>

<span>// Output: Do you like mangoes?</span>
<span>let</span> result = text.<span>replace</span>(regex, <span>'mangoes'</span>);
<span>console</span>.<span>log</span>(result);
</code>

replaceAll()

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>let</span> regex = <span>/apples/gi</span>;
<span>let</span> text = <span>'Here are apples and apPleS'</span>;

<span>// Output: Here are mangoes and mangoes</span>
<span>let</span> result = text.<span>replaceAll</span>(regex, <span>"mangoes"</span>);
<span>console</span>.<span>log</span>(result);
</code>

Regex in PHP

Functions

- -
preg_match() Performs a regex match
preg_match_all() Perform a global regular expression match
preg_replace_callback() Perform a regular expression search and replace using a callback
preg_replace() Perform a regular expression search and replace
preg_split() Splits a string by regex pattern
preg_grep() Returns array entries that match a pattern

preg_replace

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>$str</span> = <span>"Visit Microsoft!"</span>;
<span>$regex</span> = <span>"/microsoft/i"</span>;

<span>// Output: Visit QuickRef!</span>
<span>echo</span> <span>preg_replace</span>(<span>$regex</span>, <span>"QuickRef"</span>, <span>$str</span>);
</code>

preg_match

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>$str</span> = <span>"Visit QuickRef"</span>;
<span>$regex</span> = <span>"#quickref#i"</span>;

<span>// Output: 1</span>
<span>echo</span> <span>preg_match</span>(<span>$regex</span>, <span>$str</span>);
</code>

preg_matchall

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>$regex</span> = <span>"/[a-zA-Z]+ (\d+)/"</span>;
<span>$input_str</span> = <span>"June 24, August 13, and December 30"</span>;
<span>if</span> (<span>preg_match_all</span>(<span>$regex</span>, <span>$input_str</span>, <span>$matches_out</span>)) {

 <span>// Output: 2</span>
 <span>echo</span> <span>count</span>(<span>$matches_out</span>);

 <span>// Output: 3</span>
 <span>echo</span> <span>count</span>(<span>$matches_out</span>[<span>0</span>]);

 <span>// Output: Array("June 24", "August 13", "December 30")</span>
 <span>print_r</span>(<span>$matches_out</span>[<span>0</span>]);

 <span>// Output: Array("24", "13", "30")</span>
 <span>print_r</span>(<span>$matches_out</span>[<span>1</span>]);
}
</code>

preg_grep

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>$arr</span> = [<span>"Jane"</span>, <span>"jane"</span>, <span>"Joan"</span>, <span>"JANE"</span>];
<span>$regex</span> = <span>"/Jane/"</span>;

<span>// Output: Jane</span>
<span>echo</span> <span>preg_grep</span>(<span>$regex</span>, <span>$arr</span>);
</code>

preg_split

<button style="opacity: 0;"><svg height="1em" fill="currentColor" viewBox="0 0 16 16"><path
        fill-rule="evenodd"
        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
        fill-rule="evenodd"
        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>$str</span> = <span>"Jane\tKate\nLucy Marion"</span>;
<span>$regex</span> = <span>"@\s@"</span>;

<span>// Output: Array("Jane", "Kate", "Lucy", "Marion")</span>
<span>print_r</span>(<span>preg_split</span>(<span>$regex</span>, <span>$str</span>));
</code>

Regex in Java

Styles

First way

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>Pattern</span> <span>p</span> <span>=</span> Pattern.compile(<span>".s"</span>, Pattern.CASE_INSENSITIVE);
<span>Matcher</span> <span>m</span> <span>=</span> p.matcher(<span>"aS"</span>);
<span>boolean</span> <span>s1</span> <span>=</span> m.matches();
System.out.println(s1); <span>// Outputs: true</span>
</code>

Second way

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>boolean</span> <span>s2</span> <span>=</span> Pattern.compile(<span>"[0-9]+"</span>).matcher(<span>"123"</span>).matches();
System.out.println(s2); <span>// Outputs: true</span>
</code>

Third way

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>boolean</span> <span>s3</span> <span>=</span> Pattern.matches(<span>".s"</span>, <span>"XXXX"</span>);
System.out.println(s3); <span>// Outputs: false</span>
</code>

Pattern Fields

- -
CANON_EQ Canonical equivalence
CASE_INSENSITIVE Case-insensitive matching
COMMENTS Permits whitespace and comments
DOTALL Dotall mode
MULTILINE Multiline mode
UNICODE_CASE Unicode-aware case folding
UNIX_LINES Unix lines mode

Methods

Pattern

  • Pattern compile(String regex [, int flags])
  • boolean matches([String regex, ] CharSequence input)
  • String[] split(String regex [, int limit])
  • String quote(String s)

Matcher

  • int start([int group | String name])
  • int end([int group | String name])
  • boolean find([int start])
  • String group([int group | String name])
  • Matcher reset()

String

  • boolean matches(String regex)
  • String replaceAll(String regex, String replacement)
  • String[] split(String regex[, int limit])

There are more methods …

Examples

Replace sentence:

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>String</span> <span>regex</span> <span>=</span> <span>"[A-Z\n]{5}$"</span>;
<span>String</span> <span>str</span> <span>=</span> <span>"I like APP\nLE"</span>;

<span>Pattern</span> <span>p</span> <span>=</span> Pattern.compile(regex, Pattern.MULTILINE);
<span>Matcher</span> <span>m</span> <span>=</span> p.matcher(str);

<span>// Outputs: I like Apple!</span>
System.out.println(m.replaceAll(<span>"pple!"</span>));
</code>

Array of all matches:

<button style="opacity: 0;"><svg height="1em" fill="currentColor"
                                                                                  viewBox="0 0 16 16"><path
                        fill-rule="evenodd"
                        d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path
                        fill-rule="evenodd"
                        d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path> </svg></button><code><span>String</span> <span>str</span> <span>=</span> <span>"She sells seashells by the Seashore"</span>;
<span>String</span> <span>regex</span> <span>=</span> <span>"\\w*se\\w*"</span>;

<span>Pattern</span> <span>p</span> <span>=</span> Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
<span>Matcher</span> <span>m</span> <span>=</span> p.matcher(str);

List<String> matches = <span>new</span> <span>ArrayList</span><>();
<span>while</span> (m.find()) {
 matches.add(m.group());
}

<span>// Outputs: [sells, seashells, Seashore]</span>
System.out.println(matches);
</code>

Regex in MySQL

Functions

Name Description
REGEXP Whether string matches regex
REGEXP_INSTR() Starting index of substring matching regex
(NOTE: Only MySQL 8.0+)
REGEXP_LIKE() Whether string matches regex
(NOTE: Only MySQL 8.0+)
REGEXP_REPLACE() Replace substrings matching regex
(NOTE: Only MySQL 8.0+)
REGEXP_SUBSTR() Return substring matching regex
(NOTE: Only MySQL 8.0+)