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
Related
I am trying to search this coincidence in a string:
1. I need to take only numbers after the chracter '#' as long as this coincidence has not spaces, for example:
String = 'This is a test #VVC345RR, text, and more text 12345';
I want to take only this from my string -> 345.
My example:
$s = '\"access_token=103782364732640461|2. myemail#domain1.com ZmElnDTiZlkgXbT8e3 #DD234 4Jrw__.3600.1281891600-10000186237005';
$matches = array();
$s = preg_match('/#([0-9]+)/', $s, $matches);
print_r($matches);
This only works when I have one # and numbers.
Thanks!
Maybe:
#\D*\K(\d+)
Accomplishes what you want?
This will look for an #, any non-numbers, and then capture the numbers. The \K ignores the early match.
https://regex101.com/r/gNTccx/1/
I'm unclear what you mean by has not spaces, there are no spaces in the example string.
Consider the following string:
' "z":"100", "a":"+0.5", "b":"-0.578", "c":"-.5", "d":".55", "e":"-5",
"f":"01234", "g":"0.999", "i":"153.35" '
I'm trying to convert every number in this string while retaining the negative sign, except the ones starting with a 0 that arent float. In other words, I'm trying to convert to
' "z":100, "a":0.5, "b":-0.578, "c":-.5, "d":.55, "e":-5, "f":"01234", "g":0.999, "i":153.35 '
This is what I have so far
preg_replace('/((?:\:")(?:[\+])?[\-]?([0-9\.]+?)[0-9]+(?:"))/', '$1', $string);
Which isn't enough.. The hard part is to keep the " around 01234 while removing them for the other numbers..
Basically I'd like to ignore
(\:"[0][0-9]+")
Regexr link
You can use the regex (:)"(?!0\d+")\+?(-?(?:\d*\.)?\d+)" and replace it with $1$2.
See ideone
$str = '"z":"100", "a":"+0.5", "b":"-0.578", "c":"-.5", "d":".55", "e":"-5", "f":"01234", "g":"0.999", "i":"153.35", "0":"5"';
$pattern = '/(:)"(?!0\d+")\+?(-?(?:\d*\.)?\d+)"/';
print(preg_replace($pattern, '$1$2', $str));
You can use this regex to match all the numbers between quotes:
(?<=")(?!0\d)[+-]?\d*\.?\d+(?=")
EDIT: Sorry misread your question, I updated the regex to match every number but the ones beginning with 0 that aren't floats
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);
I need to extract a project number out of a string. If the project number was fixed it would have been easy, however it can be either P.XXXXX, P XXXXX or PXXXXX.
Is there a simple function like preg_match that I could use? If so, what would my regular expression be?
There is indeed - if this is part of a larger string e.g. "The project (P.12345) is nearly done", you can use:
preg_match('/P[. ]?(\d{5})/',$str,$match);
$pnumber = $match[1];
Otherwise, if the string will always just be the P.12345 string, you can use:
preg_match('/\d{5}$/',$str,$match);
$pnumber = $match[0];
Though you may prefer the more explicit match of the top example.
Try this:
if (preg_match('#P[. ]?(\d{5})#', $project_number, $matches) {
$project_version = $matches[1];
}
Debuggex Demo
You said that project number is 4 of 5 digit length, so:
preg_match('/P[. ]?(\d{4,5})/', $tring, $m);
$project_number = $m[1];
Assuming you want to extract the XXXXX from the string and XXXXX are all integers, you can use the following.
preg_replace("/[^0-9]/", "", $string);
You can use the ^ or caret character inside square brackets to negate the expression. So in this instance it will replace anything that isn't a number with nothing.
I would use this kind of regex : /.*P[ .]?(\d+).*/
Here is a few test lines :
$string = 'This is the P123 project, with another useless number 456.';
$project = preg_replace('/.*P[ .]?(\d+).*/', '$1', $string);
var_dump($project);
$string = 'This is the P.123 project, with another useless number 456.';
$project = preg_replace('/.*P[ .]?(\d+).*/', '$1', $string);
var_dump($project);
$string = 'This is the P 123 project, with another useless number 456.';
$project = preg_replace('/.*P[ .]?(\d+).*/', '$1', $string);
var_dump($project);
use explode() function to split those
I have a string Trade Card Catalogue 1988 Edition I wish to remove everything apart from 1988.
I could have an array of all letters and do a str_replace and trim, but I wondered if this was a better solution?
$string = 'Trade Card Catalogue 1988 Edition';
$letters = array('a','b','c'....'x','y','z');
$string = str_to_lower($string);
$string = str_replace($letters, '', $string);
$string = trim($string);
Thanks in advance
Regular expression?
So assuming you want the number (and not the 4th word or something like that):
$str = preg_replace('#\D#', '', $str);
\D means every character that is not a digit. The same as [^0-9].
If there could be more numbers but you only want to get a four digit number (a year), this will also work (but obviously fails if you there are several four digit numbers and you want to get a specific one) :
$str = preg_replace('#.*?(\d{4,4}).*#', '\1', $str);
You can actually just pass the entire set of characters to be trimmed as a parameter to trim:
$string = trim($string, 'abc...zABC...Z ' /* don't forget the space */);