Regex help

Tagged: 

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of Tony Antony Tony Antony 4 months, 1 week ago.

  • Author
    Posts
  • #61795
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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

  • #61798
    Profile photo of Tony Antony
    Tony Antony
    Participant

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

    http://regexstorm.net/tester

    ^\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.

    http://rubular.com/

  • #61804
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Changed regex from

    ^\d+=\d+

    to

    ^\d+=\d+$

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

  • #61813
    Profile photo of Ron
    Ron
    Participant

    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
    
  • #61815
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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.

  • #61836
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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,

  • #61855
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    Tony, Why you doesn't try my code for you ?
    you still try to solve already solved tasks 🙂

  • #61870
    Profile photo of Ron
    Ron
    Participant

    $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.

  • #61890
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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

You must be logged in to reply to this topic.