s+ and S+ meaning in Powershell

jskfan
jskfan used Ask the Experts™
on
s+ and S+ meaning in Powershell

Can someone explain the usage of  S+ and s+ in powershell, example:
$pattern = '(-username\s+)\S+'

Open in new window


Thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
The backslash belongs to the s/S as "\s"/"\S" - it's a RegEx class
\s - White-space
\S - Non-white-space
+ is a quantifier meaning one or more repetitions of the previous character.

(-username\s+)\S+'
Will match the literal "-username", followed by at least one white-space (\s+), followed by at least one non-white-space.^

There are lots of guides for Regular Expressions available.
Two from Microsoft and their .NET implementation of RegEx:

Regular Expression Language - Quick Reference
https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference

.NET Regular Expressions
https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions

At the bottom of the page, there's a nice overview of the most important stuff; direct link:
Regular Expressions - Quick Reference (download in PDF format)
https://download.microsoft.com/download/D/2/4/D240EBF6-A9BA-4E4F-A63F-AEB6DA0B921C/Regular%20expressions%20quick%20reference.pdf
AlexA lack of information provides a lack of a decent solution.

Commented:
s+ and S+ are regular expressions

https://www.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html

he \s (lowercase s) matches a whitespace (blank, tab \t, and newline \r or \n). On the other hand, the \S+ (uppercase S) matches anything that is NOT matched by \s,

Author

Commented:
per this link:    https://www.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html

\s, \S: ANY ONE space/non-space character. For ASCII, whitespace characters are [ \n\r\t\f]

$pattern = '(-username\s+)\S+'
s+ : means there is one or more spaces after  -username  
backslash \   not sure  about the second  Backslash ?
S+ means ignore spaces after the next string
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Again: the backslash is part of the character class "white-space"/"non-white-space": it's \s and \S
"s+" just means one literal "s" followed by any number of "s".
The + is a general quantifier and not part of the class.
And \S+ does not mean "ignore spaces". It means "match any non-white-space characters (but at least one)".
So
foo bar -username jdoe
will match, and "capture" everything, starting with "-username", until the end of the line; that is, "-username jdoe".
That's the string that will then later be replaced with "-username NewName"

Author

Commented:
I guess I got this part for now.

Thank you

Author

Commented:
Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial