Reputation: 907
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
Reputation: 4598
Your Regex has a lot of different mistakes...
^
there+
in your character group [a-zA-Z]
, hence only able to match a single character_
in your character group, hence it won't match Admin_RegistrationResponse
\
and d{2}
would match dd
only..
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
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