Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi 
i am having issue in extracting full filename which has  2 dot(.).  


    (ABC_A.*\.)+.*  


  Filenames for reference.

ABC_A_CommunityRollover_Autocreate_Community.12345678-1.out
ABC_A_CommunityRollover_Autocreate_Community.88345678-1.out
ABC_A_CommunityRollover_Autocreate_Community.99945678-1.out


What I have tried:

(ABC_A.*\.)+.*


although above command is working but i need a alternate solution without asterix in regex.  can anyone help me out in alternate regex command to extract full filename without asterix?
Posted
Updated 2-Sep-21 9:27am

Not really - the asterix in a regex means "zero or more repetitions of" so unless you know how many to expect (and can use "{}" syntax instead), or there will be a minimum of one character (in which case you can use "+" instead) you probably need to repeat things to get a working match.

Start by defining exactly what you are trying to match: what is "legal" and what isn't with examples, and remember that "." is a special character in a Regex which matches any other single character - so any filename which much have at least two "dots" will probably need both of them escaped to get a match.

Get a copy of Expresso[^] - it's free, and it examines, tests, and generates Regular expressions.
 
Share this answer
 
Does the teacher also forbid using '+' to replace the asterisks inside of the match expression??
This some other ways, but not capturing the last '.' to be the filename, since it starts the extension.
These are exactly like OriginalGriff is describing, so its just something to experiment with...
^(ABC_A_.+)\.[^.]{3,4}$
^(ABC_A_.{1,})\.[^.]{3,4}$
^(ABC_A_.{1,})\.[a-z]{3,4}$
^(ABC_A_.{1,})\.(?i)[a-z]{3,4}$
^(ABC_A_.{1,})\.(?i)[\w]{3,4}$

If only needing to conduct filenames with exactly two '.' they could look like...
^(ABC_A_[^.]+)\.[^.]+\.[^.]{3,4}$
^(ABC_A_[^.]{1,})\.[^.]+\.[^.]{3,4}$
^(ABC_A_[^.]{1,})\.[^.]+\.[a-z]{3,4}$
^(ABC_A_[^.]{1,})\.[^.]+\.(?i)[a-z]{3,4}$
^(ABC_A_[^.]{1,})\.[^.]+\.(?i)[\w]{1,5}$

These do look more complicated, but its just replacing . with [^.] and then using [^.]+\. just before the extension.
You could change {1,} into {0,} but if he wont grant using the asterisks, Im thinking he will also not like this either.
This one is for filenames like in the example, but its making -1 optional, and granting 3-or-4 letter extensions.
^(ABC_A_[^.]+\.\d+(-\d)?)\.[a-zA-Z]{3,4}$

This why OriginalGriff is saying to define what is 'legal' with examples, because otherwise its much guessing.
Our teacher does also discourage using .* whenever .+ can match all of the samples, and its a 2-point deduction.
So one time I was being sneaky and did use .{0,} instead, but then he just gives me the deduction anyway!
 
Share this answer
 
Quote:
(ABC_A.*\.)+.*
although above command is working

Matching what you want is a good start, but also not matching what you don't want is better.
Try something like : (ABC_A(.*\.){2}).*

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900