locked
Using regular expressions for parsing string RRS feed

  • Question

  • Hello guys, 

    I'm trying to parse the string in the following format: "1.1.1. Text" - to get number and text separately in different variables.

    So I tried this way:

    -match "(?<Number>.*)\. (?<Text>.*)" 

    $Num=$matches['Number']

    $Txt=$matches['Text'] 

    And this works awesome, until I meet the following string(of course): "1.2.3. Text. Text1"

    As I already noticed, powershell checks for matches "from the behind", so my previous command will return something like this:

    $Num: 1.2.3. Text

    $Text: Text1

    Are there any ways to "tell" powershell to check matches from string start to string end, not vice versa? 

    If not, how can I parse all the numbers before point+space in the string start of the other text?

    I tried -match (?<Number>.*)\d\. (?<DepartmentName>.*) 
    but, as expected, this comand won't return the last figure... May be there is also an option to "include" those signs in the result("\d\. ")?
    Help me please..

    Best regards.

    Anton


    Friday, December 28, 2012 11:04 AM

Answers

  • Are there any ways to "tell" powershell to check matches from string start to string end, not vice versa? 

     

    The dotnet "flavor" of regex does have a "RightToLeft" option, but it's considered "buggy" and it's recommended that you avoid using it if possible.  In this case there  appear to be better options.

    http://social.msdn.microsoft.com/Forums/en/regexp/thread/045ec9f7-a839-40e8-9caf-66bfcc3f6311


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by Aiden_Cao Thursday, January 3, 2013 6:22 AM
    Friday, December 28, 2012 1:43 PM

All replies

  • how about: split by spaces
    take first element as $Num and rest elements as $Text?

    Friday, December 28, 2012 11:13 AM
  • -match "(?<Number>(\d\.)+) (?<Text>.*)"
    • Marked as answer by Aiden_Cao Thursday, January 3, 2013 6:22 AM
    • Unmarked as answer by Martynenko Anton Thursday, January 10, 2013 10:22 AM
    Friday, December 28, 2012 11:15 AM
  • Are there any ways to "tell" powershell to check matches from string start to string end, not vice versa? 

     

    The dotnet "flavor" of regex does have a "RightToLeft" option, but it's considered "buggy" and it's recommended that you avoid using it if possible.  In this case there  appear to be better options.

    http://social.msdn.microsoft.com/Forums/en/regexp/thread/045ec9f7-a839-40e8-9caf-66bfcc3f6311


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by Aiden_Cao Thursday, January 3, 2013 6:22 AM
    Friday, December 28, 2012 1:43 PM