Regex from String

Hi,

I have such strings:
Work Company TKA-A120
Work Company TKA-120

I want to get two values.

  1. TKA for both cases
  2. 120 but only for second string when there are three digits without letter before β€œ-”. If not no Regex found.

I dont know how to find such regex to be working for these strings :frowning:

HI @Marek_Matuszak try this regex pattern

Let me know if it works for you

Regards
Nived N
Happy Automation

grafik
grafik

i want something more like 3 upper cases in first case

Hey @Marek_Matuszak,

If all strings always follow the pattern you mentioned above, I’d use a Split and TryParse. Something like this:

Int32.TryParse(stringVar.Split("-"c)(1)).

If output is true it means there is no letter after hyphen. Then, you can use stringVar.Split("-"c)(1) to get 120, for example.

Considering output as false, you can use same expression to get A120, for example. But I understood you don’t need this one.

To retrieve the letters before hyphen, for both cases you could use:

stringVar.Split(" β€œc).Last.ToString.Split(”-"c)(0)

In addition, you can follow this idea using Invoke Method. It would be much better :slight_smile:

Also, regex is another way. If you want to use it, feel free.

Hi @Marek_Matuszak

Thank you for providing a Sample, the expected Output and a little about the Pattern (more information on the pattern is always better)

How does this pattern work for you?
(?<=Work Company )([A-Z]{3})(-\d{3})*
Preview / play with the results here :slight_smile:

Description: It will always grab the three capital letters after the words β€œWork Company” and IF followed by a β€œ-” and exactly three digits it will grab those too.

image

If you want to learn Regex - check out my Regex Megapost for new users :slight_smile:

2 Likes