Regex to find last word from a line in text file

Hi All, Could you guys please help me with this regex expression.

I have two text file. I want to use regex to find first letter of last word in below text: (Only from first sentence) – that would be find C from “C24722SBCOMPO”

2037636300717009930801216252777 00029027304Test Bank - USD 30 C24722SBCOMPO
4061350300873609930801216252777 00000252150Test Bank - USD 309 F09280FIB

And another regex to find: 5 from “501457A TAYLOR” in line below
77261815910660099308012162537788 00000045950Test Bank - USD 101 501457A TAYLOR

Hi @RamboRocky

You can use the below Regular expression for both Input data,

(?<=-\s[A-Z]+\s+\d+\s+).*

Check the below steps,

- Assign -> Input = "2037636300717009930801216252777 00029027304Test Bank - USD 30 C24722SBCOMPO"

- Assign -> Output = System.Text.RegularExpressions.Regex.Match(Input.ToString, "(?<=\-\s[A-Z]+\s+\d+\s+).*").Value.ToString

Hope it helps!!

Hi @RamboRocky

Try this

(From line In inputString.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
          Let parts = line.Split(" "c)
          Where parts.Length > 2
          Select parts.Last()).FirstOrDefault()

Regards,

Hi,

2037636300717009930801216252777 00029027304Test Bank - USD 30
C24722SBCOMPO
4061350300873609930801216252777 00000252150Test Bank - USD 309
F09280FIB
77261815910660099308012162537788 00000045950Test Bank - USD 101
501457A TAYLOR

I have spaces in my text so it reads the data with the spaces infront of the output string. How do I remove the spaces infront?

Could you be more specific… @RamboRocky

Sorry this space infront?

Okay got it… @RamboRocky

Try the below expression which will not take the space before the output data,

(?<=-\s[A-Z]+\s+\d+\s+)[A-Za-z0-9]+.*

Hope it helps!!

1 Like

Thanks mate it works. Much Appreciated :slight_smile:

1 Like

It’s my pleasure… @RamboRocky

Happy Automation!!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.