When using a specific delimiter in a data string, often I want to trim off the last instance of it before exploding.
I have always wondered this:
PHP trim() will trim off whitespace, or certain characters. But what if I wanted to trim full strings, like so:
$data = 'cookieDELIMITERchocolateDELIMITER';
$data = trim($data, 'DELIMITER');
The above won't really work, since it won't trim the string "DELIMITER", but instead trim the characters "D,E,L,I,M,T,R".
What I want is this:
$data = 'cookieDELIMITERchocolate';
I appreciate your suggestions!
$data = 'cookieDELIMITERchocolateDELIMITER';
$data = preg_replace('/^(DELIMITER)*|(DELIMITER)*$/', '', $data);
var_dump($data);
// string(24) "cookieDELIMITERchocolate"
The regular expression ^(DELIMITER)*|(DELIMITER)*$ will match 0+ (*) instances of "DELIMITER" at the beginning (^) of a string or (|) the end ($) of the string. preg_replace() will take these matches and replace them with blank strings.
Full RegEx Explanation
^ # Matches the start of the string
(DELIMITER)* # Matches "DELIMITER" 0+ times, thanks to the *
| # OR
(DELIMITER)* # Matches "DELIMITER" 0+ times, thanks to the *
$ # Matches the end of the string
This means it will either match many DELIMITERs at the beginning OR the end of the string. Since preg_replace() does a global match/replace, it will get all occurrences of DELIMITER as long as they are either attached to the beginning or end of the string.
Adding the dot (^(DELIMITER).*|(DELIMITER)*$) would match the beginning of the string, followed by DELIMITER, followed by 0+ of any character (. matches all). This would effectively replace the entire string if it started with DELIMITER.
If you are using this to keep from having an empty array element after explode(), try this:
$array = array_filter(explode('DELIMITER', $data))
Related
I am trying to extract a number from a string. The string is a filename, will be variable in its contents, but will end with a space followed by a number.
So strings like a b 1212, a 1212. I am not sure if filenames ever end up with a whitespace at the beginning, but something like 1212 should not produce a match.
I am using so far
preg_match('#^(.)* (\d*)$#', $str, $matches);
but that returns a match for case of something like 1212.
I thought about reversing the string and doing this
preg_match('#^(\d*)(\s*)(.*)$#', strrev($str), $matches);
var_dump(strrev($matches[1]));
and checking if $matches[1] != '' and that seems to work, but I am trying to better understand regex, so that I can do this without the reverse, and figure out proper pattern for the case get number at end of string, which is always preceded by a space, and where that space in turn is always preceded but anything.
Ideas?
You can try this approach:
preg_match("/^\S.* (\b\d+)$/", $str, $matches);
echo end($matches)."\n";
For instance if you use the following variable:
$str = "1234 lkjsdhf ldjfh 1223";
The call to end($matches) will return 1223
Whereas if you use the following variable:
$str = " 1212";
call to end($matches) will remain empty.
You may use this regex:
^\S.*\b(\d+)$
RegEx Demo
RegEx Details:
^: Start
\S: Match a non-whitespace
.*: Match 0 or more of any character
\b: Word boundary
(\d+): Match any integer number in capture group #1
$: End
I need help with this preg match. I tried this from other post but did not get the result. So finally posting it.
I am trying to extract z,a,b from first and a from second example.
1) Write a function operations with parameter z,a,b and returns b.
2) write a function factorial with parameter a.
This is what I tried so far:
preg_match_all('/\parameter(.*?)\and?/', $question, $match);
$questionVars = $match[1];
print $questionVars;
Thank you so much!
Your solution can be different depending on actual requirements.
If you need a string after parameter as a whole word that can consist of word and comma chars you may use
preg_match('~\bparameter\s+\K\w+(?:\s*,\s*\w+)*~', $s, $m)
See the regex demo. The \bparameter\s+ matches a word boundary, parameter and 1+ whitespace chars, and all this text is omitted with the help of \K, the match reset operator. \w+(?:\s*,\s*\w+)* matches and returns the 1+ word chars followed with 0+ repetitions of a comma enclosed with optional whitespace chars and again 1+ word chars.
If you plan to get those comma-separated chunks separately, use
preg_match_all('~(?:\G(?!^)\s*,\s*|\bparameter\s+)\K\w+~', $s, $m)
See another regex demo. Here, (?:\G(?!^),\s*|\bparameter\s+) will either match the whole word parameter with 1+ whitespace after (\bparameter\s+, as in the previous solution) or the end of the previous successful match with , enclosed with optional whitespace chars (\G(?!^)\s*,\s*). The \K will omit the text matched so far and \w+ will grab the value. You may replace with [^,]* to grab 0+ chars other than a comma.
I have string:
$string = "1\n2\n3\n4\n\n\n";
And pattern:
$pattern = '/\s*$/';
// \s* - any spaces
// $ - end of string
Why when I call:
preg_replace($pattern, "\n5", $string);
Output is:
"1\n2\n3\n4\n5\n5"
What is wrong with my pattern and how to change it, that the result will be:
"1\n2\n3\n4\n5"
$ can match both the impending linebreak, as well as the end of a string.
Your main issue in this case however is \s* matching zero or more whitespace characters.
For instance \n\nā is matched and replaced first by \s*$
As well as the 5\nā after the first replacement.
The \s*$ gets treated as $ simply.
And the new end of text ā still matches $.
It's not like $ by itself always matches twice, but the combination of \s*$ can.
The simplest fix is not to match for zero spaces, but make it only eat up existing whitespace:
/\s+$/
That's sufficient in your case, as you only care about the end of the string and cleaning out excess linebreaks anyway.
Could someone help with a preg_match expression I need it to match the - dash character at the start and end of a string. This is for tags e.g. match -my-tag- should then be my-tag so It only matches the start and end of a string and replace it the characters with and empty string
You can do that with this easy expression:
$string = "-my-tag-";
$tag = preg_replace("/^-(.*)-$/", "$1", $string);
^ and $ are used to match the start and the end of the string, while (.*) captures every other symbols.
You can read more about regular expressions in the official PHP Documentation.
i need to write a regex for make a double check: if a string contains empty spaces at the beginning, at the end, and if all string it's composed by empty spaces, and if string contains only number.
I've write this regex
$regex = '/^(\s+ )| ^(\d+)$/';
but it doesn't' work. What's wrong ?
First things first: get your spaces right!
For example (\s+ ) will match a minimum of one space (\s+) followed by another space ()! Same applies for the space between | and ^. This way you will match the space literally every time and this leads to wrong results.
If I get you right and you want to match on strings which
start with one or more spaces OR
end with one or more spaces OR
consist only of spaces OR
consist only of numbers
I'd use
/^(?:\s+.*|.*\s+$|\d+$)/
Demo # regex101
This way you match spaces at the start of the string (\s+.*) or (|) spaces at the end of the string (.*\s+$) or a completely numeric string (\d+$).
Insert capturing groups as needed.
This will match in case the whole string consists of spaces, too, because technically the string then starts with spaces.
The space before ^(\d+) make your regex can't catch the numeric string.
It should be like below:
$regex = '/^\s*\d*\s*$/';
First if all, remove the space between | and ^. You are trying to match a space before the beginning of the line (^), so that can not work.
I do not exactly understand what you want. Either a string that only consists of white spaces, or a number that may have white spaces at the beginning or end? Try this:
$regex = '/^\s*\d*\s*$/';