Powershell – Regular Expressions for Parse Match or Replace


Regular expressions

List
Regular expression list:

https://positivedb.wordpress.com/wp-admin/post-new.php#edit_timestamp

https://positivedb.wordpress.com/wp-admin/post-new.php#edit_timestamp

RegEx Description
. matches any character except newline
\ escape character
\w word character [a-zA-Z_0-9]
\W non-word character [^a-zA-Z_0-9]
\d Digit [0-9]
\D non-digit [^0-9]
\n new line
\r carriage return
\t tabulation
\s white space
\S non-white space
^ beginning of a line
$ end of a line
\A beginning of the string (multi-line match)
\Z end of the string (multi-line match)
\b word boundary, boundary between \w and \W
\B not a word boundary
\< beginning of a word
\> end of a word
{n} matches exaclty n times
{n,} matches a minimum of n times
{x,y} matches a min of x and max of y
(a|b) ‘a’ or ‘b’
* matches 0 or more times
+ matches 1 or more times
? matches 1 or 0 times
*? matches 0 or more times, but as few as possible
+? matches 1 or more times, but as few as possible
?? matches 0 or 1 time
[abc] a single character of: a, b, or c
[^abc] any single character except: a, b, or c
[a-z] any single character in the range a-z
[a-zA-Z] any single character in the range a-z or A-Z
(…) capture everything enclosed
a? zero or one of a
a* zero or more of a
a+ one or more of a
a{3} exactly 3 of a
a{3,} 3 or more of a
a{3,6} between 3 and 6 of a

Match a single character (returns a True):

‘positivedb’ –match ‘posit.vedb’

Match at least one of a range of characters (returns a True):

‘positivedb and powershell’ –match ‘positiv[a-g] and powershell’

Doesn’t match at least one of a range of characters (returns a True):

‘positivedb and powershell’ –match ‘positiv[^h-z] and powershell’

Match any but these characters, can be specified in one or more contiguous ranges (returns False):

‘positivedb and powershell’ –match ‘positiv[^abc-dfg-hij] and powershell’

The some as above but returning True:

‘positivedb and powershell’ –match ‘positiv[^abc-dfg] and powershell’

Match only if at the beginning of the line (returns ‘ivedb and powershell’):

‘positivedb and powershell’ –replace ‘^posit’,’’

Match only if at the end of the line (returns ‘positivedb and power’):

‘positivedb and powershell’ –replace ‘shell$’,’’

Match zero or more of * preceding character (returns True):

‘positivedb and powershell’ –match ‘z*’
‘positivedb and powershell’ –match ‘z*z*’

The some as above but returns False:

‘positivedb and powershell’ –match ‘^z*$’

Match one or more of preceding and succeeding + (returns True):

‘positivedb and powershell’ –match ‘posi+’

Match a group of characters replaced by . (returns True):

‘positivedb and powershell’ –match ‘posi.’
‘positivedb and powershell’ –match ‘posi.shell’
‘positivedb and powershell’ –match ‘posi.shell$’

 

In Depth
http://www.zerrouki.com/powershell-cheatsheet-regular-expressions/
http://ss64.com/ps/syntax-regex.html

Tested With
PowerShell 4.0

Advertisements
Categories: Powershell | Leave a comment

Post navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: