I'm trying to change a bunch of decimals in a string to two decimal points. The regex seems to match it just fine. The problem is with the replace.
This is my code:
$input_lines = "-33.873293252 151.201538015999972,-33.873175 151.201689183999946";
print preg_replace("/[0-9]+(\.[0-9][0-9]?)?/", "$0 $2", $input_lines);
Which outputs decimal that I want | truncated decimals that I don't want:
-33.87 3293252 151.20 1538015999972 ,-33.87 3175 151.20 1689183999946
So I tried changing the replacement to $0. But now the replace stopped working, and is instead giving me:
-33.873293252 151.201538015999972,-33.873175 151.201689183999946
How can I rewrite my regular expression so it gives me the desired output?
Better:
preg_replace("/(?<=\.\d\d)\d+/","",$input_lines);
Replaces all trailing decimals after the first two with nothing.
([-+]?\d+(?:\.\d{2})?)(\d*)
Try this.Replace by $1.See demo.
https://regex101.com/r/vD5iH9/46
$re = "/([-+]?\\d+(?:\\.\\d{2})?)(\\d*)/m";
$str = "-33.873293252 151.201538015999972,-33.873175 151.201689183999946";
$subst = "$1";
$result = preg_replace($re, $subst, $str);
Related
I have a regex which removes everything and leaves just numbers and a dot. It doesn't work for large numbers.
Eg. It works when the £5.99 is put into it i get 5.99
but for bigger numbers like £48.49 I get .49
I want this to work with numbers as big as £100.99
/[^0-9.]+([0-9]{2}){0,1}/
An input would be something like "this costs £25.95."
The result should be 25.95
You can use
'~(\d+(?:\.\d+))|.~s'
and replace with \1.
See regex demo
This regex replacement will keep only integer and float numbers in the string.
See IDEONE demo:
$re = '~(\d+(?:\.\d+))|.~su';
$str = "this costs £25.95.";
$result = preg_replace($re, '\1', $str);
echo $result;
// => 25.95
How would I extract all alpha characters (including space) like for example:
#john camel07 st.doe!
where I only want to get john camel stdoe.
I tried using the regex from this another SO question but it does not work.
$re = "/[^a-zA-Z ]+/";
$str = "#john camel07 st.doe!";
$subst = "";
$result = preg_replace($re, $subst, $str);
You can simply replace by empty string all non alpha and space characters.See demo.
https://www.regex101.com/r/rL8wP1/7
If your data contains unicode, this should work a bit better:
echo preg_replace("/[^[:alpha:][:space:]]/ui", '', '#john camel07 st.doe!');
Borrowed with a change from https://stackoverflow.com/a/659030/1935500
I'm looking for a RegEx for preg_replace in PHP for the following scenario:
example string "Bjerre- Jonas, Jorgensen- Silas, Wohlert- Johan, Madsen- Bo"
desired string "Jonas Bjerre, Silas Jorgensen, Johan Wohlert, Bo Madsen"
string is a csv field and double quotes are enclosures and are part of string
any number of occurrences may exist including none - the example clearly has 4
there is a consistent - to match on separating matches to be swapped
I'm a noob at PHP and RegEx and have been playing around in the cool test arena with things like preg_replace("/^\"(?<=- )/", ""$2 $1$3"", $input_lines); with horrible results. Thanks for help!
([^," -]*)\s*-\s*([^," ]*)
Try this.See demo.
http://regex101.com/r/hI0qP0/20
$re = "/([^\", -]*)\\s*-\\s*([^,\" ]*)/m";
$str = "\"Bjerre- Jonas, Jorgensen- Silas, Wohlert- Johan, Madsen- Bo\"";
$subst = "$2 $1";
$result = preg_replace($re, $subst, $str);
I want to replace all consecutive characters in each WORD if there are more than three (three being the most possible in German language, two for English so I know the output example is grammatically wrong).
Example input:
Hellooooo Louis, whaaaaaat's up pal?
Expected output:
Hellooo Louis, whaaat's up pal?
I tried to change:
preg_replace('/(\w)\1+/', '$1', $word);
to
preg_replace('/(\w)\3+/', '$1', $word);
However, it doesn't output anything.
You can use the following regex:
((\w)\2{2})\2+
See demo
Replace with $1.
IDEONE:
$re = "#((\w)\\2{2})\\2+#";
$str = "Hellooooo Louis, whaaaaaat's up pal?";
$subst = "$1";
$result = preg_replace($re, $subst, $str);
echo $result;
Output:
Hellooo Louis, whaaat's up pal?
EXPLANATION:
We capture the symbol with (\w) - it is Group 2 value. Then, we check if it is followed by the same character with \2{2} exactly 2 times, and we capture it into Group 1. Then, we match any more identical subsequent characters with the \2 backreference.
Here is a way to go:
preg_replace('/((\w)\2\2)\2+/', '$1', $word);
Also you can use \K for resetting after and replace with empty, which is a bit more efficient:
(\w)\1\1\K\1+
See regex101
I need to remove
[0037][user name]
combination from a sentence. In the first brackets always containing numbers
eg:
[0032]
Digit count will not exceed than 4 by any chance. In the second brackets always containing letters eg:
[first name]
anyone have an idea how to do this?
You can use preg_replace() to implement regular expression syntax and try the following expression.
$str = preg_replace('/\[\d+]\[[a-z ]+]/i', '', $str);
\[\d{1,4}\]\[[a-zA-Z ]+\]
This should do it.Replace by empty string.See demo.
http://regex101.com/r/oE6jJ1/22
$re = "/\\[\\d{1,4}\\]\\[[a-zA-Z ]+\\]/im";
$str = "asdas asdsad [1234][asd asd] asdasd";
$subst = "";
$result = preg_replace($re, $subst, $str);