0

I am attempting to parse a regex formula in PowerShell and not having any luck. I've created the Regex and have tested it works on RegExr although when I attempt to execute a match query on it it returns no results.

The Regex is looking for any occurrence of a pattern such as below (including the TWO blank line spaces between the Price and the Address.:

$9,999,999


26 Fake Street, Fake Island, ABC 9999

my regex:\$[\d]{1},[\d]{3},[\d]{3}\n\n\n\d{1}.*?, ([A-Z])\w+ [[\d]{4}

My PowerShell code is as Below:

$Webcontent = Get-Content 'C:\Utilities\Content.txt' -Raw
[regex]::Match($WebContent,'\$[\d]{1},[\d]{3},[\d]{3}\n\n\n\d{1}.*?, ([A-Z])\w+ [[\d]{4}').Groups.Value | Out-File C:\utilities\NewContent.txt

Is it this query possible and also can it return ALL occurrences of this when it finds it?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
djsnakz
  • 468
  • 2
  • 6
  • 15

2 Answers2

1

You can use the following regular expression:

(?m)s*\$\d{1},\d{3},\d{3}\s*(?:\r\n|\r|\n)+\d+.*?, ([A-Z][a-zA-Z]*)\s+\d{4}

See demo

All the matches are returned in the global $matches variable that is set by the -match operator. Please see more on this at regular-expressions.info.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you, very helpful. Is there a switch or something i might be able to add to make my Regex search the whole document and return all occurrences rather than grabbing the first one and outputting that? [regex]::Match($WebContent,'(?m)s*\$\d{1},\d{3},\d{3}\s*(?:\r\n|\r|\n)+\d+.*?, ([A-Z][a-zA-Z]*)\s+\d{4}').Groups.Value | Out-File C:\utilities\Amount.txt – djsnakz May 09 '15 at 10:12
  • Please check this post: http://stackoverflow.com/questions/24835401/powershell-regular-expression-multiple-matches Does that solution work for you? – Wiktor Stribiżew May 09 '15 at 11:05
0

To match newlines you need to match carriage return character as well as new line character in order \r\n. So you just need to change \n\n\n to \r\n\r\n\r\n

revo
  • 47,783
  • 14
  • 74
  • 117