Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression only match if String ends with target

Tags:

regex

I need a regular expression that will only match to the String if it ends with the target that I am looking for. I need to locate a file with a specific extension, problem is this extension also comes in other files. For example I have two files named

B82177_2014-07-08T141507758Z.ccf 

and

B82177_2014-07-08T141507758Z.ccf.done 

I only want to grab the first of these and my pattern is:

.*\.ccf 

but this grabs both.

Any suggestions appreciated, I am a newbie to regular expressions.

like image 614
AbuMariam Avatar asked Jul 08 '14 16:07

AbuMariam


People also ask

What does '$' mean in regex?

Literal Characters and Sequences For instance, you might need to search for a dollar sign ("$") as part of a price list, or in a computer program as part of a variable name. Since the dollar sign is a metacharacter which means "end of line" in regex, you must escape it with a backslash to use it literally.

Does regex work only with strings?

So, yes, regular expressions really only apply to strings. If you want a more complicated FSM, then it's possible to write one, but not using your local regex engine.


2 Answers

Use an end anchor ($):

.*\.ccf$ 

This will match any string that ends with .ccf, or in multi-line mode, any line that ends with .ccf.

like image 109
p.s.w.g Avatar answered Sep 28 '22 11:09

p.s.w.g


$ is used to match the end of the string. and can be used like

"string"$ 

like

xyz$ 

if you want to end with xzy

like image 45
Shahir Ansari Avatar answered Sep 28 '22 12:09

Shahir Ansari