Shridhar Sangamkar
Shridhar Sangamkar

Reputation: 87

PowerShell Regex for searching in two lines

I am trying for regular expression for matching the following line in powershell

Add :
Some text here

Only two lines here it need to search,

But as of now I got the following regular expression which is searching the whole paragraph. I am stuck here please anyone help me

Add :+[\u000A]*[a-zA-Z0-9]*

Code snippet for the regex is as follows:

 $RegEx = "Add :+[\u000A]*[a-zA-Z0-9]*" 
    $requestforregex = "Requested for : [a-zA-Z0-9 \(\)\-]*" 
   $matchedItems = [regex]::matches($ticket[1].Body.Text.replace('&','&'), $RegEx,[system.Text.RegularExpressions.RegexOptions]::Singleline)
   $requestefor =( [regex]::matches($ticket[1].Body,$requestforregex,[system.Text.RegularExpressions.RegexOptions]::Singleline))[0].Value.Replace("Requested for : ","")

Upvotes: 1

Views: 276

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You need to use

$RegEx = "Add\s*:[\r\n]+.*"

See the regex demo. Here, the [\r\n]+ will match 1 or more CR or LF symbols and .* will match any 0+ chars other than a newline. Note you cannot use Singleline option you used in your code.

To match the text at the start of a line, add (?m)^:

$RegEx = "(?m)^Add\s*:[\r\n]+.*"

where (?m) is the inline version of the RegexOptions.Multiline modifier option.

Upvotes: 1

Related Questions