John W. Mnisi
John W. Mnisi

Reputation: 907

Regular expression extract filename from line content

I'm very new to regular expression. I want to extract the following string "109_Admin_RegistrationResponse_20130103.txt"

from this file content, the contents is selected per line:
01-10-13 10:44AM 47 107_Admin_RegistrationDetail_20130111.txt
01-10-13 10:40AM 11 107_Admin_RegistrationResponse_20130111.txt

The regular expression should not pick the second line, only the first line should return a true.

Upvotes: 0

Views: 96

Answers (3)

luiges90
luiges90

Reputation: 4598

Your Regex has a lot of different mistakes...

  1. Your line does not start with your required filename but you put an ^ there
  2. missing + in your character group [a-zA-Z], hence only able to match a single character
  3. does not include _ in your character group, hence it won't match Admin_RegistrationResponse
  4. missing \ and d{2} would match dd only.
  5. As per M42's answer (which I left out), you also need to escape your dot . too, or it would match 123_abc_12345678atxt too (notice the a before txt)

Your regex should be

\d+_[a-zA-Z_]+_\d{4}\d{2}\d{2}\.txt$

which can be simplified as

\d+_[a-zA-Z_]+_\d{8}\.txt$

as \d{2}\d{2} really look redundant -- unless you want to do with capturing groups, then you would do:

\d+_[a-zA-Z_]+_(\d{4})(\d{2})(\d{2})\.txt$

Upvotes: 3

Lalith B
Lalith B

Reputation: 12239

I'm a newbie in php but i think you can use explode() function in php or any equivalent in your language.

$string = "01-09-13 10:17AM 11 109_Admin_RegistrationResponse_20130103.txt";
$pieces = explode("_", $string);

$stringout = "";
foreach($i = 0;$i<count($pieces);i++){
 $stringout = $stringout.$pieces[$i];
}

Upvotes: 0

Toto
Toto

Reputation: 91430

Remove the anchors and escape the dot:

\d+[a-zA-Z_]+\d{8}\.txt

Upvotes: 1

Related Questions