Regex help

I have a list of string such as:
String one
String two
1=67
2=65
3=66
4=69
5=64
6=70
7=68
Another String

I need to find the last occurrence of of this string (starting with a number = ending with a number). In this case, 7=68

What’s the regex for this? This is what I have, but not working. If I type in 7=68 it finds it.

^ start of a string with \d a digit or more + containing =

Select-String -AllMatches "^\d+=" -SimpleMatch

http://www.regexlib.com/CheatSheet.aspx

Thank you,

Tony

I think I found it because when I type 7=68 here as in put it finds it

^\d+=\d+

But, I applied that it in script, but it’s still not finding it.

I also tested it here, and it works, but just not in my actual script.

Changed regex from

^\d+=\d+

to
^\d+=\d+$

Added $ to state end of string, but still same result. I’m out of ideas.

Switch to .NET regex, you have better control.

$s=@'
String one
String two
1=67
2=65
3=66
4=69
5=64
6=70
7=68
Another String
'@

$m=[regex]::Matches($s,'^\d+=\d+','Multiline')
$m[$m.count-1].value

Thank you, I have the strings reading from line to line instead of in a @

such as

$search = $Read[$start…$end]
$m=[regex]::Matches($Search,‘^\d+=\d+’,‘Multiline’)
$m[$m.count-1].value

But now, it’s finding the wrong text.

Any other ideas? I still think my original regex condition is good because when I manually do it, it finds the correct regex.

Thank you,

Tony, Why you doesn’t try my code for you ?
you still try to solve already solved tasks :slight_smile:
https://powershell.org/forums/topic/reading-and-saving-file-taking-too-long/#post-60778

$search = $Read[$start…$end]|Out-String

$Read[subset] creates an array. You could loop through the array and find the last matching element or convert it to a multiline string and search it once to return all the matches.

Thank you for everyone’s valuable input. I made some changes, and

$Search | Where-Object{$_ -cmatch "^\d+=\d+$"}

This is working for me, and it takes about a second to create and add a person to each calendars. Before it was taking about 5 minutes.

Thank you,

Tony