0

I for the life of me cannot figure out how to negate everything but what I want to capture with REGEX.

I get close with [^(\d{4}-\d{3}-\d{3}]

But doing a replace in powershell with an input of: 1234-567-899 ABC 1W(23W) BLUE BIKE30 KIT

I get: 1234-567-8991(2330

Instead of 1234-567-899

  • [^...] defines a negated class. None of the specified characters is matched - this is not you want, but rather probably a negative lookaround is required, but your question is ambiguous. What do you want to negate? – TonyR Aug 28 '20 at 07:08
  • I think i found the answer, i wanted to negate everything but the numbers at the beginning. (?<=\d{4}-\d{3}-\d{3})(.*$) seems to work. – Ryan Jamison Aug 28 '20 at 07:13

3 Answers3

1

You could also use regex -replace to remove everything except for the stuff you want to keep.

In your example

"1234-567-899 ABC 1W(23W) BLUE BIKE30 KIT" -replace '^([-\d]+).*', '$1'

would return 1234-567-899

Theo
  • 57,719
  • 8
  • 24
  • 41
0

Please try with (?:^\d{4}-\d{3}-\d{3})(.*)$

Liju
  • 2,273
  • 3
  • 6
  • 21
0

To match any text after <4-DIGITS>-<3-DIGITS>-<3-DIGITS> pattern at the start of the string, you may use

(?<=^\d{4}-\d{3}-\d{3}).*

See the regex demo. Details:

  • (?<=^\d{4}-\d{3}-\d{3}) - a positive lookbehind that matches a location that is immediately preceded with
    • ^ - start of string
    • \d{4} - four digits
    • - - a hyphen
    • \d{3}-\d{3} - three digits, -, three digits
  • .* - any 0 or more chars other than newline chars, as many as possible
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563