How to use preg_match to match numbers between hyphens? - php

I need to validate strings like this:
$string = 'test3-10-2';
I need the penultimate number between hyphens, so in this case '10'. These are other examples:
$string2 = 'test45-50-178-1'; //match = 178
$string3 = 'test45-580-89-12-1'; //match = 12
Can you help me?

this RegExp should do the trick using a positive lookahead: (change 4 to whatever max num of digits you want to allow)
\d{1,4}(?=-\d{1,4}$)

You could also use the explode function (which is like a split):
$items = explode("-",$yourString);
end($items);
$whatYouWant = prev($items);

Related

php remove the leftmost number in string

For some reason, I want to remove the leftmost number inside the whole string.
Here are the example:
0abc > abc
23abc > abc
123abc456 > abc456
123ab1ab123 > ab1ab123
99abc1a1a4 > abc1a1a4
Is it possible do in php?
I tried to use:
$words = preg_replace('/[0-9]+[a-z]/', '', $file);
but it will remove both the number and the first alphabet.
You can just use an anchor and remove the [a-z] part from your regex:
$words = preg_replace('/^[0-9]+/', '', $file);
and if you dont like Rizier123 aswer:
$words = ltrim('123ab1ab123','0123456789');
or
$words = ltrim('123ab1ab123','0..9');
trims all numbers from left, probably faster than are regular expression also

preg_replace issue. Character class inside other character class

I have a fragment in text like
;flat
CID_999999 = 99999999
CID_999999 = 99999999
CID_999999 = 99999999
CID_999999 = 99999999
where 999999 are some numbers. I want to find this fragment by regexp.
When I use this regex:
preg_replace('/;flat[\s\r\n]+[CID_0-9]+\s=\s[0-9]+\n/','REPLACEMENT',$content);
it replaces ;flat and first CID string.
So, I suppose that if I put [CID_0-9]+\s=\s[0-9]+\n part in character class []+it will remove all CID strings. But if I do it it doesn't replace anything. So I don't understand something in regex. What am I doing wrong?
I thought that expected output is clear, but ok. I should replace all fragment by REPLACEMENT.
Use lookbehind if you are trying to match just the digits after the = and before the newline
For more accurate matching use CID[0-9]+ rather than [CID0-9]+
preg_replace('/(?<;flat[\s\r\n]+CID_[0-9]+\s=\s)[0-9]+(?=\n)/','REPLACEMENT',$content);
You don't need a character class ([...]); you need a group ((...)). Your code should look like this:
preg_replace('/;flat[\s\r\n]+([CID_0-9]+\s=\s[0-9]+\n?)+/', 'REPLACEMENT', $content);
Note the ? at the end, just in case your last line isn't terminated with a new line (\n) character.
Demo
;flat\s*|(?!^)\G(CID_\d+\s*=\s*\d+\s*)
Try this.Replace by $1.See demo.
https://regex101.com/r/gQ3kS4/7
$re = "/;flat\\s*|(?!^)\\G(CID_\\d+\\s*=\\s*\\d+\\s*)/";
$str = ";flat\nCID_999999 = 99999999\nCID_999999 = 99999999\nCID_999999 = 99999999\nCID_999999 = 99999999\n;";
$subst = "$1";
$result = preg_replace($re, $subst, $str);

Get only part of string

I have a string like this:
$myString = "line40";
Where the value 40 is variable (I have a set of random numbers).
I would like to get ONLY the number 40 (or any other random numer - 40 is just an example).
Just like this:
$newString = 40;
I tried to do using a regular expression but I failed... Please, someone can help me?
Thanks a lot.
$newString = preg_replace('/\D/', '', $myString);
The regular expression \D matches anything that isn't a digit. This replaces them all with nothing.
try preg_match()
$myString = 'line40';
preg_match('/([0-9])+/', $myString, $m);
echo $m[0]; // 40
How about this, You can use filters
$myString = "line40";
$myString = filter_var($myString, FILTER_SANITIZE_NUMBER_INT);
echo $myString; //output: 40
Ref: http://php.net/manual/en/filter.filters.sanitize.php

How to extract a collection of numbers from a string?

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

Php string split, how can i do that?

$mystring = "#blablabla Kayit Ol ogrencino:1176160"
This is my string, placament of ogrencino:1176160 in string can change, but others will be stable.
Like:
$mystring = "#blablabla ogrencino:1176160 Kayit Ol" etc.
How can i parse "1176160"?
Use preg_match like so:
$mystring = "#blablabla ogrencino:1176160 Kayit Ol";
// Changed regular expression to match Kolink's comment
preg_match('/(?<=\bogrencino:)\d+/', $mystring, $matches);
print_r($matches);
// displays Array ( [0] => 1176160 )
If it appears more than once in the string you can do preg_match_all()
You can also look at this regex:
(?<=ogrencino:)(.+?)[\s$]
It is independent of what value exists after ogrencino:. (It can be digit or non-digit)
Regex break up:
(?<=ogrencino:) = Positive look behind, checks `ogrencino:` exists behind the next criteria.
.+? = Any thing (except new line) one or more time, but lazy due to `?`.
[\s$] = after match either whitespace or end of line will exists.
You want to look into regular expressions, particularly preg_match.
For your specific example do something like this:
$mystring = "#blablabla ogrencino:1176160 Kayit Ol";
$matches = array();
preg_match( '/ogrencino\d+/', $mystring, $matches);
echo $matches[0]; // ogrencino:1176100
What this does is find every instance of "one or more digits" (that's what the \d+ is) and read every match into $matches
Without REGEX:
$string = "#blablabla ogrencino:1176160 Kayit Ol";
$beg = strpos($string,"ogrencino:") + strlen("ogrencino:");
$end = strpos($string," ",$beg);
$mynumber = substr($string,$beg,$end-$beg); //1176100

Categories