I'm having a hard time getting the regex pattern that can get the 123456abcde_ and it's also optional on the filename? I'm using the preg_replace and one of the requirements is the pattern that can get the 123456abcde_ and replace it with an empty string, btw here is my code and sample pattern of the filename.
123456abcde_get-thisValue_more details - specific_20200728173715594600.zip
123456abcde_get-thisValue_more details_20200728173715594600.zip
123456abcde_getthisValue_more details - specific_20200728173715594600.zip
123456abcde_getthisValue_more details_20200728173715594600.zip
123456abcde_get-thisValue_20200728173715594600.zip
123456abcde_getthisValue_20200728173715594600.zip
get-thisValue_more details - specific_20200728173715594600.zip
get-thisValue_more details_20200728173715594600.zip
getthisValue_more details - specific_20200728173715594600.zip
getThisValue_more details_20200728173715594600.zip
get-thisValue_20200728173715594600.zip
getthisValue_20200728173715594600.zip
And this is my code and the target output of this is the get-thisValue or getthisValue.
$filename = '123456abcde_get-thisValue_more details - specific_20200728173715594600.zip';
$pattern = '/^([\da-z])\1*_/m';
$prefix = preg_replace($pattern, '', $filename);
$prefix = substr($prefix, 0, strpos($prefix, '_'));
echo $prefix;
Thanks in advance.
CodePudding user response:
You should try
$pattern = '/^([\da-z] _)?([^_] )_.*$/';
$prefix = preg_replace($pattern,'$2', $filename);
echo $prefix;
The first part within parenthesis with the question mark behind it is the optional prefix consisting of one or more digits or lowercase characters followed by an underline character. The question mark behind the closing parenthesis makes this group optional, meaning it matches the empty string if there is no prefix.
The second part within parenthesis stands for 'one or more characters not equal to the underline character' matching your desired "get-thisValue" or "getthisValue".
Finally the following underline character and the '.*' match the rest of the filename all the way to the end (indicated by the $ at the end of the pattern).
So we have a pattern, that matches the complete file name and picks up the interesting part (between or before the underline character, depending on wether we have a prefix) in the second group.
Thus the preg_replace function replaces the complete filename with just what was matched for group 2.
I can't give any guarantee this is right down to the last character, since I have no way of testing this right now, but I am pretty sure it works.
CodePudding user response:
The changes in the code is $pattern = '/^123456abcde_/m';
.If the string is already known then you need to simply match the with the hardcoded string which want to replace.
<?php
$filename = '123456abcde_get-thisValue_more details - specific_20200728173715594600.zip';
$pattern = '/^123456abcde_/m';
$prefix = preg_replace($pattern, '', $filename);
$prefix = substr($prefix, 0, strpos($prefix, '_'));
echo $prefix;
?>
CodePudding user response:
if you only want to get the value:
^(?:[\da-z] _)?\K[^_\n]
$re = '/^(?:[\da-z] _)?\K[^_\n] /m';
$str = '123456abcde_get-thisValue_more details - specific_20200728173715594600.zip
123456abcde_get-thisValue_more details_20200728173715594600.zip
123456abcde_getthisValue_more details - specific_20200728173715594600.zip
123456abcde_getthisValue_more details_20200728173715594600.zip
123456abcde_get-thisValue_20200728173715594600.zip
123456abcde_getthisValue_20200728173715594600.zip
get-thisValue_more details - specific_20200728173715594600.zip
get-thisValue_more details_20200728173715594600.zip
getthisValue_more details - specific_20200728173715594600.zip
getThisValue_more details_20200728173715594600.zip
get-thisValue_20200728173715594600.zip
getthisValue_20200728173715594600.zip
123456abcde_get-thisValue_more details - specific_20200728173715594600.zip';
preg_match_all($re, $str, $matches);
print_r($matches[0]);
Output
Array
(
[0] => get-thisValue
[1] => get-thisValue
[2] => getthisValue
[3] => getthisValue
[4] => get-thisValue
[5] => getthisValue
[6] => get-thisValue
[7] => get-thisValue
[8] => getthisValue
[9] => getThisValue
[10] => get-thisValue
[11] => getthisValue
[12] => get-thisValue
)