Reputation: 1971
I have this sentence
C:\\wamp\\www\\callCenter\\joomlatools-files\\docman-files\\test.pdf
And I want to get as a result of my regex exression test but without using another word from the sentence.
My regex is ~\\\(.+)\.pdf~
but I have wamp\www\portail-callcenter\joomlatools-files\docman-files\test
as a result.
Can someone please explain how to get it done?
Upvotes: 0
Views: 100
Reputation: 10350
I'm not sure what are you trying to get. But I guess you want to get the word of test
from your first sentence. So use this pattern:
~\\\\(\w+)\.pdf$~
And then $1
containing what you need.
~
delimiter\\\\
matches two backslashes literally(
capturing group $1
(which is containing what you need to get)\w+
matches one or more letter, number or underscore\.
matches a dot literally$
end of stringHere is your pattern in the question ~\\\(.+)\.pdf~
. The problem of your pattern is .+
. Because it means one or more character(s) (every character even \
). So your pattern matches that two backslashes which are in the beginning of your string, and the rest of backslashes will be match as every character (.+
).
To avoid this, you have to use \w
which means any letter, number or underscore instead. In this case the matched part will be just test
.
Upvotes: 2
Reputation: 6702
<?php
$subject = 'C:\\\\wamp\\\\www\\\\callCenter\\\\joomlatools-files\\\\docman-files\\\\test.pdf';
echo $subject; // C:\\wamp\\www\\callCenter\\joomlatools-files\\docman-files\\test.pdf
preg_match_all('~\\\\\\\\([^\\\\]+)\.pdf$~', $subject, $match);
var_dump($match);
result
array (size=2)
0 =>
array (size=1)
0 => string '\\test.pdf' (length=10)
1 =>
array (size=1)
0 => string 'test' (length=4)
If you want to match a double backslash, you need 8 backslashes in your regex. In the first level the string input in the script line is parsed and backslashes are considered as meta chararacters to escape the following character. As result 4 slashes remain in your regex. When executing the regex, the backslashes are treated as well as meta characters, so 2 slashes remain to be compared with the subject string.
echo '~\\\\\\\\([^\\\\]+)\.pdf$~'; // ~\\\\([^\\]+)\.pdf$~
Upvotes: 1
Reputation: 42087
To get test
from C:\\wamp\\www\\callCenter\\joomlatools-files\\docman-files\\test.pdf
:
(?<=\\\\)[^.\\]+(?=\.[^.\\]+$)
You can avoid lookarounds by using a group to get the desired portion:
\\\\([^.\\]+)\.[^.\\]+$
Upvotes: 1
Reputation: 8332
Your regex is almost correct. Only thing is it matches every type of character in your final word. Change it to
~\\\\([^\\]+)\.pdf~
This matches everything but a \
in the final word - and you should get the file name only.
Regards
Upvotes: 1