In previous post, there was discussion about groups and alternation used in regex. This post explains about common shorthand symbols and inline modifiers in regex.
There are many shorthand symbols available in regex. Here is a list of few of them
|\n||A new line|
|\r||A carriage return|
|\s||A whitespace character (vertical space, horizontal space, new line etc.)|
|\S||A non-whitespace character (equivalent to [^s])|
|\d||A digit(equivalent to [0-9])|
|\D||A non-digits(equivalent to [^0-9] or [^\d])|
|\w||An alphanumeric or _(equivalent to [a-z0-9A-Z_])|
|\W||Anything other than non alphanumeric or _(equivalent to [^a-z0-9A-Z_] or [^\w])|
While most of the symbols are self-explanatory, \b needs an explanation. e.g. regex cat will be matched in string tom cat as well as tomcatx. On other hand if regex used is \bcat\b, it will match tom cat but won’t match tomcatx or tomcat or cattom. So \b acts as word boundary and is of zero-width i.e. it do not consumes any string.
Positions of word boundary include:
- Start of string
- End of string
- Between two characters where one is word character( \w) and other is non-word character( \W).
e.g. the positions mentioned in below string qualifies for word boundary by corresponding rules mentioned above(spaces only for clarity here)
yaaaaa - fffbs * ssxx
^ ^ ^ ^ ^ ^
| | | | | |
1) 3)3) 3)3) 2)
Inline modifiers can be used alongside regex to specify some actions. Most commonly used of them are
|(?i)||Ignore case. Matches the pattern without case sensitivity|
|(?s)||. matches new line as well as carriage return|
|(?m)||^ and $ acts as start and end of line|
Inline modifiers is used in regex as: (?i)aBCd
- If there are multiple inline modifiers, it can be specified as (?smi).*.
- Unless specified, entire string including new line is considered on which regex pattern is matched. (?m) modifier specifies that the pattern should only match within a line and not in entire string. For a string, there can be multiple lines obtained after splitting on \n.
This part explained two of the most useful concepts in regex. Next part will dive further in regex.