Which regex expression pattern to use if I want to select the number after 2 words?

Which regex expression pattern to use if I want to select the number after 2 words?

I’m using the activity matches and I want to retrieve the number only of the below text

I’m using (?<=Crew/)(.?\d)

for the below text

Ref.: P&OCrew/287

thats working fine.

however, how to proceed if I want to select the number only of the below text?

Ref.: P&OCrew/LBS/287

and basically text can change to Ref.: P&OCrew/MCCC/287 for instance or P&OCrew/MI/287
How can I skip the 2nd word in the expression?

Thanks in advannce for your reply
Rgds,
Maic

Refering to groups
grafik
(?:(?<=Crew\/)\D*?)(\d+)

this doesnt work (see below)

I only want to keep the number so => 287

and I have always the denomination "P&OCrew/
and then the initial always change it can be LBS/ or MCCC/ or MI/

Do you have an idea how to get the number only?

Thansk in advance for your precious help

Hi @mce

Try this expression

System.Text.RegularExpression.Regex.Match(“InputString”,“(?<=/)(\d+)”).Tostring.Trim

image

Regards
Gokul

we mentioned the reference to the group

so it is working, when using the group value

[CheatSheet] - System.Text.RegularExpressions | RegEx - News / Tutorials - UiPath Community Forum

Hi there @mce,
I hope you are well!

You should be able to pull it using the below:
(?<=.*\/.*\/)\d+

Ref.: P&OCrew/MCCC/287
Result: 287

image

Please let me know your thoughts.
Thanks once again for your support,
Josh

Thanks Mr_JDavey that is smart it would works if I put “/”

I’m just thinking if in the future I have this text :

/45/P&OCrew/LBS/287

and I use the following expression

(?<=/)(\d+)

then it will recognize 2 different numbers (see below)

image

How can I only recognize the number 287 ?

Thansk in advance for your help

Thats the reason why we suggested a pattern which is using Crew as an Anchor
May you help us by elaborating on why working with groups is not possible at your end.

We do see handled the case with the pattern and refering to group as e.g.

@mce As you said the existing expression might change to like this /45/P&OCrew/LBS/287 or number of slashes might vary. I am thinking, always you need the number at the end (correct me if wrong), no matter if it vary

Use the below expression which it can handle all the scenarios : \d+$

It will always get the number which is at the end

Hi @mce

Thank you for the Sample, the expected Output. But I think we need more information on the Pattern and any extra requirements you have.

There are many talented people on the UiPath Community forums who can help with a pattern and have contributed a reply (@ppr and @Gokul001) but they need as much information as possible to make a strong pattern :slight_smile: This information should be in the first post.

1 Minute invested in Original Post = 2+ Minutes saved later :hourglass_flowing_sand:

From your subsequent information/questions I have created two patterns which may suit. These patterns both rely heavily on the text “P&OCrew/” and slashes “/”.

Your Information / Requirements are:

Providing confirmation that “P&OCrew/” is a constant is valuable information.
I have assumed that there is more than three variants and not just “LBS” or “MCCC” or “MI”.

My Patterns
Pattern #1 - Preview it here

This pattern relies on Letters only (A-Z or a-z) in the second part of the expression.

Pattern #2 - Preview it here
image
This pattern is a little more dynamic but can handle no second grouping or “word”.

If you want to learn Regex - check out my Regex MegaPost.

Hopefully you have found a suitable pattern from the above replies.

Cheers

Steve

2 Likes

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