简体   繁体   中英

Regex get specific instance of string

I have a variable amount of strings that are contained in a row separated by spaces.

The last two values before the CRLF are always present and I need to extract the second to last numeric value using Regex.

I originally tried (.+?[ ]{1,}){6}.+?\\r\\n but it could be the 5th or 6 instance I need extracted.

66666666     RCA     JOBS               CASH      300,080.47 0000ABCDE\r\n

66666666     RCA     JOBS      182.09   VL          9,755.02 0000FGHIJ\r\n

You could name your groups. This should work "(.+?[ ]{1,}){5}(?<MyFirstValue>.+?[ ]{1,})(?<MySecondValue>.*)

var regex = new Regex("(.+?[ ]{1,}){5}(?<MyFirstValue>.+?[ ]{1,})(?<MySecondValue>.*)");
var test = @"66666666     RCA     JOBS               CASH      300,080.47 0000ABCDE
66666666     RCA     JOBS      182.09   VL          9,755.02 0000FGHIJ";


Console.WriteLine(string.Join("\n", 
    regex
        .Matches(test)
        .Cast<Match>()
        .Select(m => m.Groups["MyFirstValue"].Value + " " + m.Groups["MySecondValue"].Value)));

Test it here

You can use the following RegEx, if you want the second last Word:

\S+(?= \S+\r$)

It Works by selecting any non-Space chacacters followed by a Space, Some non-Space characters, a 'return' at the end of line.

Edit:

You must set the multiline option for the RegEx. Edit2:

If you only want the first line you can also use:

\S+(?= \S+\r)

(without the 'endofline')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM