1

I was using the below regex to substitute file names

Regex -> .*\/([A-Z0-9_]{1,9})_(O).*.cmd
Substitution -> $1

The file names were like below:

File Name                         | Substituted Name
---------------------------------- ------------------
/V3/OGM_REC_Offline_Level0_4D.cmd  OGM_REC
/V2/PIE_PROD_Online_Level1_6D.cmd  PIE_PROD
/V3/BR2_OnDemand.cmd               BR2
/opt/STING_Online_Inc0_1W.cmd      STING

Then the files changed and I modified the regex

Regex -> .*\/([A-Z0-9_]{1,9})(_O|Full).*.cmd
Substitution -> $1

Additional new file names

File Name             | Substituted Name
---------------------- ------------------
/opt/RSU10Full.cmd     RSU10
/V4/REZ40_1Full.cmd    REZ40_1

Now, it seems there are new files are getting updated with below name formats

/app/OMGIT_FullOnDemand_4W.cmd
/admin/FOC_STG_Full_6D.cmd

I've modified the regex again, but it's not getting successful

Regex -> .*\/([A-Z0-9_]{1,9})(_O|Full|_Full).*.cmd
Substitution -> $1
3
  • 1
    Try .*/([A-Z0-9_]{1,9}?)(_O|_?Full).*[.]cmd. Commented Nov 20, 2015 at 11:21
  • @stribizhev , Shouldn't you post this as an answer ? I've tested it and it's working for the examples that I have... I'll be doing a extensive tests using this on Monday... Thanks for your quick reply... Commented Nov 20, 2015 at 11:29
  • OK, let me post and explain. Commented Nov 20, 2015 at 11:30

2 Answers 2

1

I suggest using a version with a lazy limiting quantifier {1,9}? and optional _:

.*/([A-Z0-9_]{1,9}?)(_O|_?Full).*[.]cmd

This way, we match as few characters with [A-Z0-9_]{1,9}? as possible to return a valid captured subtext, and _?Full part can hold the optional underscore.

See the regex demo

Sign up to request clarification or add additional context in comments.

1 Comment

If you get more complicated patterns on Monday, feel free to come back here.
0

I've noticed that unnecessary tail is allways started with: (optional) _, letter in uppercase, letter in lowercase. So, universal solution is:

.*\/([^a-z]*?)[_]?[A-Z][a-z].*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.