Search code examples
phpfilepathregex-negationbackslash

php PCRE regex to get only the file name that terminates in .txt


so I am trying to form a PCRE regex in php, specifically for use with preg_replace, that will match any number of characters that make up a text(.txt) file name, from this I will derive the directory of the file.

my initial approach was to define the terminating .txt string, then attempt to specify a character match on every character except for the / or \, so I ended up with something like:

'/[^\\\\/]*\.txt$/'

but this didn't seem to work at all, I assume it might be interpreting the negation as the demorgan's form aka: (A+B)' <=> A'B'

but after attempting this test:

'/[^\\\\]\|[^/]*\.txt$/'

I came to the same result, which made me think that I shouldn't escape the or operator(|), but this also failed to match. Anyone know what I'm doing wrong?


Solution

  • Try this pattern '/\b(?P<files>[\w-.]+\.txt)\b/mi'

    $PATTERN = '/\b(?P<files>[\w-.]+\.txt)\b/mi';
    $subject = 'foo.bar.txt plop foo.bar.txtbaz foo.txt';
    preg_match_all($PATTERN, $subject, $matches);
    var_dump($matches["files"]);