0

How to write regex for following strings, have no space or multi space either pre or post.

"any"
"any4"
"any6"
" any "
" any4 "
" any6 "
"  any  "
"  any4  "
"  any6  "

I have tried "any[4,6]{0,1}$", but that works with only the first three strings.

mklement0
  • 382,024
  • 64
  • 607
  • 775
RAM
  • 11
  • 1
  • Why not use the `Trim()` operator? e.g. `$test = @("any", "any4", "any6", " any "," any4 "," any6 "," any "," any4 "," any6 ") $test | % { $_.Trim() }` (RegEx is not the best tool for any job) – Remko Jul 04 '18 at 15:26

4 Answers4

2

To match such strings in full (omit the ^ and $ for substring matching):

$strings = (
  "any",
  "any4",
  "any6",
  " any ",
  " any4 ",
  " any6 ",
  "  any  ",
  "  any4  ",
  "  any6  "
)

$strings -match '^ *any[46]? *$' # matches all of the above

To match other forms of whitespace too, use \s in lieu of  .

To extract the tokens with whitespace trimmed:

$strings -replace '^ *(any[46]?) *$', '$1' # -> 'any', 'any4', ..., 'any4', 'any6'

(...) forms a capture group, which $1 refers to in the replacement string. See this answer of mine for more.


As for what you tried:

  • [4,6] is a character set, which means that a single input character matches any of these characters, including ,, which is not your intent.

  • Your use of duplication symbol (quantifier) {0,1} is correct (0 or 1 instance of the preceding expression), but ? is a simpler form of the same construct (just like * is simpler than {0,}).

  • By placing $ (the end-of-input assertion) directly after {0,1}, you didn't allow for optional trailing spaces.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

You can try this regex

\"([a-zA-Z0-9]+)\"
Subhendu Mondal
  • 121
  • 1
  • 9
0

RegEx is not needed for ths job. You only need

$string -like '*any*'

Furthermore, it's faster.

f6a4
  • 1,684
  • 1
  • 10
  • 13
0

The reason the first 3 only show up is because you are using a $ at the end. $ infers that the END of the line is the last character you put

so

any[4-6]{0,1}$

will match

any4
any
any6
any4
any6
any

If you want to find it with white space on either end just remove the $.

If you're trying to find it based on a specific string, where white space MAY occur use

^\s*any[4-6]{0,1}\s*$

Mind you regex varies on certain systems and this code may not work on your platform, as \s may not be known etc..

Please also note that an actual SPACE i.e. ^ [4-6]{0,1} $ may not search right, and if it does, it will only search SPACES not white space, tabs etc.

But if you want to include spaces for certain you could escape them i.e. ^\ [4-6]{0,1}\ $ . In certain systems like Linux, a space in the command will make it think it's the next variable/input and won't understand what [4-6]{0,1} means

rkta
  • 3,959
  • 7
  • 25
  • 37
Robert Cotterman
  • 2,213
  • 2
  • 10
  • 19