its possible to include x option with all regexes by default
for example instead of:
$r = [regex]'(\d{3})-[a-z]\1+'
or
$r = [regex]@'
(x)
(\d{3})
-
[a-z]
\1+
'@
for more lisibility
$r = [regex]'(\d{3}) - [a-z] \1+'
or
$r = [regex]@'
(\d{3})
-
[a-z]
\1+
'@
This would break any existing regex which used " " rather than \s which covers more characters
Although attractive for the -match / -replace operators it would lead people to write expressions which behave differently in different places. (e.g. PowerShell won't change the default behavior of the underlying .NET regex engine)
It's generally accepted expressions ignore spaces only if (?x) is specified.
It's generally accepted expressions ignore spaces only if (?x) is specified.
You can even specify that inline:
$r = [regex]'(?x)
(x)
(\d{3})
\p{Zs}-\p{Zs}
[a-z]
\1+'
$r.Match('x123 - ax')
# Groups : {0, 1, 2}
# Success : True
# Name : 0
# Captures : {0}
# Index : 0
# Length : 9
# Value : x123 - ax
(Also use (?xi)
instead for case insensitive)
@SeeminglyScience that's what I meant.
In some places the only option is put (?x) in the regex itself.
In others (like the .net object) it is possible to specify single-line/multi-line case [insensitive] and ignore whitespace outside of the expression
So under what circumstances is built-in? Is there a strict law of logic?
If not, do need to provide another option.
So under what circumstances is built-in? Is there a strict law of logic?
If not, do need to provide another option.
The only option which is built in is -match and -replace are actually -iMatch and -iReplace.
Now... it would be possible to have (say) Smatch and Sreplace with are space insensitive, but these would also need C and I versions. I'm not sure it is really necessary. I think that if you learn to write regexes which are complicated enough to need to be split for readability, then you probably pickup how to specify ignore white-space as well.
Aye, I don't see a whole lot of value here in just being able to specify (?x)
in PowerShell v.s. being able to set it in the regular expression itself.
There is a very simple question. What to do if you need to match the spaces?
[ValidatePattern({^[A-Z]{3,4}})]
The default is case insensitive, which puzzles me.
Use general standard regex. Actually ignore case. Amaze me!
There is a very simple question. What to do if you need to match the spaces?
Use \p{Zs}
to match a literal space. See Supported Unicode general categories.
[ValidatePattern({^[A-Z]{3,4}})]
The default is case insensitive, which puzzles me.Use general standard regex. Actually ignore case. Amaze me!
It's to stay consistent with everything else in PowerShell. Note that [regex]'pattern'
isn't really a PowerShell thing, that's just calling the constructor. If you need it to be case sensitive (which if possible you should avoid for parameter validation) you can add (?-i)
to be beginning of the pattern.
Most helpful comment
This would break any existing regex which used " " rather than \s which covers more characters
Although attractive for the -match / -replace operators it would lead people to write expressions which behave differently in different places. (e.g. PowerShell won't change the default behavior of the underlying .NET regex engine)
It's generally accepted expressions ignore spaces only if (?x) is specified.