Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8

Function

  • Match
  • Substitution
  • List
  • Unit Tests
"
sen[t|d] from( my| a)?(\w|'|\s)+?(smart|smartphone|mobile|iphone|windows|ipad|htc)( phone| device)?
"
i
sen
matches the characters sen literally (case insensitive)
Match a single character present in the list below
[t|d]
t|d
matches a single character in the list t|d (case insensitive)
from
matches the characters from literally (case insensitive)
1st Capturing Group
( my| a)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative
my
my
matches the characters my literally (case insensitive)
2nd Alternative
a
a
matches the characters a literally (case insensitive)
2nd Capturing Group
(\w|'|\s)+?
+? matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
1st Alternative
\w
\w matches any word character (equivalent to [a-zA-Z0-9_])
2nd Alternative
'
' matches the character ' with index 3910 (2716 or 478) literally (case insensitive)
3rd Alternative
\s
\s matches any whitespace character (equivalent to [\r\n\t\f\v  ])
3rd Capturing Group
(smart|smartphone|mobile|iphone|windows|ipad|htc)
1st Alternative
smart
smart
matches the characters smart literally (case insensitive)
2nd Alternative
smartphone
smartphone
matches the characters smartphone literally (case insensitive)
3rd Alternative
mobile
mobile
matches the characters mobile literally (case insensitive)
4th Alternative
iphone
iphone
matches the characters iphone literally (case insensitive)
5th Alternative
windows
windows
matches the characters windows literally (case insensitive)
6th Alternative
ipad
ipad
matches the characters ipad literally (case insensitive)
7th Alternative
htc
4th Capturing Group
( phone| device)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative
phone
phone
matches the characters phone literally (case insensitive)
2nd Alternative
device
device
matches the characters device literally (case insensitive)
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
i

Test String