Regex Problem With Similar Pattern

I have the following pattern,
AXDAAM904A-FQR004
ADAEM100A-FKR019
CTDAGM264A-FGR0076

and I wish to extract only a portion of it, i.e., the highlighted portions have to be trimmed off.

DAAM904A-FQ
DAEM100A-FK
DAGM264A-FG

I am facing an issue when trying to detect the A and AX using Lookbehind. It tells me that the quantifier has to be fixed, so I am not able to zero into A while, I can do it easily for CT and AX.

image

Help would be most appreciated, thanks in advance!

Kind Regards,
Ashwin A.K

Hi,

Can you try the following?

System.Text.RegularExpressions.Regex.Match(yourString,"[A-Z]{4}\d{3}[A-Z]-[A-Z]{2}").Value

Regards,

1 Like

HI @Yoichi ,

Thank you for your fast response, this works like a charm but I forgot to mention one more point,

CT, A, AX are all prefixes while R and whatever comes after that is a Subfix.

CTXXXXXXRYYYY

X and Y is not fixed in size, which is why I decided to go for that approach.
Also, I only need whats inbetween the Prefixes and Subfixes which come in varying formats
I will show few more examples:

AXDAEM100A-FKR005
ASAE10C-FKR0190
CTVAE1B-GKR2500

Is there any way to look Ahead of AX|A|CT and look behind R?

Kind Regards,
Ashwin A.K

Hi,

How about the following?

System.Text.RegularExpressions.Regex.Match(yourString,"((?<=AX|A)[^X]|(?<=CT))\w+-[^R]+").Value

Or the following might be better if there is no R in target string.

System.Text.RegularExpressions.Regex.Match(yourString,"((?<=AX|A)[^X]|(?<=CT))[^R]+").Value

Regards,

1 Like

Thank you so much @Yoichi -san!

I really appreciate it!

Kind Regards,
Ashwin A.K

1 Like

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