Hi I'm trying to create a script in powershell that finds a string in a file and then modifies it but I'm having issues matching the string I want.
So, I'm trying to make sure that I can find this type of string
|d where d is is any digit, the string must not be in single or double quotes.
SO I'd like to match |1 or |12 or |333
So far this is what I have:
\|[0-9]+ is returning strings with or without single quotes around it, so |1 and also '|5';
\'\|[0-9]\'+ returns only strings with single quotes around it, so matches '|4' but not |3
\\'|'(?:\\'|[^'])*'|(\[0-9]+) as the first one, it is returning strings with or without single quotes around it, so |1 and also '|5';
\[^']\|[0-9][?']\+ I thought this would say something like exclude ' at the beginning and at the end of the string, but it doesn't work at all
Now, you might have understood that I really don't know much about regex, so I'm trying options in the online regex checker here https://regex101.com/ but it appears to be behaving slightly different than powershell.
Now, what do I have to do to match a string without the quotes? I don't necessarily want the solution, but perhaps some hint would be appreciated :-)

Recommended Answers

"Windows PowerShell is a programming language from Microsoft that is primarily designed for system administration. Since PowerShell is built on top of the .NET framework, .NET’s excellent regular expression support is also available to PowerShell programmers."

Now, try using .NET's sequence (?!subexpr) and (?<!subexpr).

(?!subexpr) for ex. \w+\b(?![,:;]) will find …

Jump to Post

All 3 Replies

Here's a short bit from the web about Powershell oddities in this regard:

  • backtick to escape instead of \
  • regex must be in "double quotes"
  • must either use single quotes or escapes to have capture groups work in the replacement string

There may be more but my answer is yes that powershell may not match regex101. Either make a cheat sheet of how powershell differs or search out more about how powershell differs.

Thanks for that :-). This escaping sequence it interesting, I didn't come across that but this \|[0-9]+ escapes the '|' absolutely fine! So I'm a bit confused...

commented: The version of powershell might be why. Note: I'm not a historian on this area. +15

"Windows PowerShell is a programming language from Microsoft that is primarily designed for system administration. Since PowerShell is built on top of the .NET framework, .NET’s excellent regular expression support is also available to PowerShell programmers."

Now, try using .NET's sequence (?!subexpr) and (?<!subexpr).

(?!subexpr) for ex. \w+\b(?![,:;]) will find a word that is not followed by coma, colon or semicolon

(?<!subexpr) for example (?<!,)\b\w+will find a word that is not preceeded by a coma

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.