How can I export the 4 from string:
9 buy property stocks over 4 days
I can export the numbers by doing:
$numbers = preg_replace('/[^0-9]+/', '', $string);
but I just want the number that is followed by days?
You may use a a matching regex with preg_match:
if (preg_match('/\d+(?=\s*days)/', $string, $m)) {
echo $m[0];
}
See the PHP demo and a regex demo. The \d+(?=\s*days) pattern matches 1+ digits (\d+) that are followed with 0+ whitespaces (\s*) and a substring days.
The same regex with a capturing group will look like
if (preg_match('/(\d+)\s*days/', $string, $m)) {
echo $m[1];
}
See another PHP demo and another regex demo. Here, the 1+ digits are captured into a group and can be accessed via $m[1].
You can use:
$days = preg_replace('/.*(\d+) days/i', '\1', $string);
Regex Demo
PHP Demo
Related
I'm trying to grab everything after the following digits, so I end up with just the store name in this string:
full string: /stores/1077029-gacha-pins
what I want to ignore: /stores/1077029-
what I need to grab: gacha-pins
Those digits can change at any time so it's not specifically that ID, but any numbers after /stores/
My attempt so far is only grabbing /stores/1
\/stores\/[0-9]
I'm still trying, just thought I would see if I can get some help in the meantime too, will post an answer if I solve.
You may use
'~/stores/\d+-\K[^/]+$~'
Or a more specific one:
'~/stores/\d+-\K\w+(?:-\w+)*$~'
See the regex demo and this regex demo.
Details
/stores/ - a literal string
\d+ - 1+ digits
- - a hyphen
\K - match reset operator
[^/]+ - any 1+ chars other than /
\w+(?:-\w+)* - 1+ word chars and then 0+ sequences of - and 1+ word chars
$ - end of string.
See the PHP demo:
$s = "/stores/1077029-gacha-pins";
$rx = '~/stores/\d+-\K[^/]+$~';
if (preg_match($rx, $s, $matches)) {
echo "Result: " . $matches[0];
}
// => Result: gacha-pins
You should do it like this:
$string = '/stores/1077029-gacha-pins';
preg_match('#/stores/[0-9-]+(.*)#', $string, $matches);
$part = $matches[1];
print_r($part);
I need to output the single 3 in the array below using preg_match or preg_split, how can I accomplish this? This possibilities are 1 through 8.
VMName Count CompatibilityForMigrationEnabled CompatibilityForOlderOperatingSystemsEnabled ------ ----- -------------------------------- -------------------------------------------- ap-1-38 3 False False
I have tried the following with no success using both preg_match and preg_split:
('\\s+\\d\\s+', $output)
('\\s+[\\d]\\s+', $output)
("^[\s0-9\s]+$", $output)
("/(.*), (.*)/", $output)
Give the following preg_match a try
<?php
$matched = preg_match('/( [0-9] )/', $string, $matches);
if($matched){
print_r($matches);
}
Hope this helps!
Try this:
preg_match("/( \d{1} )/", $input_line, $output_array);
Examples: http://www.phpliveregex.com/p/luf
To match a 1 to 8 number that is in between whitespaces, you may use
preg_match('~(?<!\S)[1-8](?!\S)~', $s, $match)
See the regex demo.
Details
(?<!\S) - a whitespace or start of string required immediately to the left of the current location
[1-8] - a digit from 1 to 8
(?!\S) - a whitespace or end of string required immediately to the right of the current location
See PHP demo:
$str = 'VMName Count CompatibilityForMigrationEnabled CompatibilityForOlderOperatingSystemsEnabled ------ ----- -------------------------------- -------------------------------------------- ap-1-38 3 False False';
if (preg_match('/(?<!\S)[1-8](?!\S)/', $str, $match)) {
echo $match[0];
}
// => 3
Note you may also use a capturing approach:
if (preg_match('/(?:^|\s)([1-8])(?:$|\s)/', $str, $match)) {
echo $match[1];
}
See the regex demo and the PHP demo.
Here, (?:^|\s) is a non-capturing alternation group matching start of string *(^) or (|) a whitespace (\s), then a digit from 1 to 8 is captured (with ([1-8])) and then (?:$|\s) matches the end of string ($) or a whitespace. $match[1] holds the necessary output.
Hi i'm trying to replace all digits or numbers in a string except digits after dash by blank space
For example I have this :
$string = "1234 Example-1234";
And I want to have only "Example-1234"
I tried preg_replace('/\-?\d+/','',$string); but even digits after dash are replaced
Edited: Thanks everyone i tried all of your answers and it works well !
If you want to just skip all digits preceded with - and remove all others, use
'~-\d+(*SKIP)(*F)|\d+~'
See the regex demo
Note you would like to trim the result or add \s* around \d+ pattern.
Pattern details:
-\d+(*SKIP)(*F) - match -, 1+ digits and skip this match
| - or
\d+ - 1 or more digits
See the PHP demo:
$str = '1234 Example-1234';
$res = preg_replace('/-\d+(*SKIP)(*F)|\s*\d+/', '', $str);
echo trim($res); // => Example-1234
The solution using regex negative lookbehind assertion (?<!a)b:
$str = "1234 Example-1234";
$str = preg_replace('/(?<![0-9-])\d+/', '', $str);
print_r($str);
The output:
Example-1234
Because you are looking for words that contain a dash, you can achieve this by to splitting the string via spaces, loop through the array values until you find a string with a dash, and then output from there onwards.
$string = "1234 Example-1234";
$words = explode(" ", $string);
foreach($words as $word) {
if (strpos($word, '-') !== false) {
echo $word;
break; // delete this line if there are multiple instances of words with dashes in your string
}
}
This will output Example-1234.
You can see a working example here
ONLINE Regex tester : https://regex101.com/r/ykUWfM/3
<?php
$string = "1234 Example-1234";
echo preg_replace('/-(\d+)/','',$string);
?>
OUTPUT: before - convert into string
1234 Example
NOTE: Before - its get the string before dash -
OR
Demo: https://regex101.com/r/ykUWfM/2
<?php
$string = "1234 Example-1234";
echo preg_replace('/(?<![0-9-])\s*\d+/','',$string);
?>
OUTPUT:
Example-1234
Hello I need a regex to get a string "trkfixo" from
SIP/trkfixo-000072b6
I was trying to use explode but I prefer a regex solution.
$ex = explode("/",$sip);
$ex2 = explode("-",$ex[1]);
echo $ex2[0];
You may use '~/([^-]+)~':
$re = '~/([^-]+)~';
$str = "SIP/trkfixo-000072b6";
preg_match($re, $str, $match);
echo $match[1]; // => trkfixo
See the regex demo and a PHP demo
Pattern details:
/ - matches a /
([^-]+) - Group 1 capturing 1 or more (+) symbols other than - (due to the fact that [^-] is a negated character class that matches any symbols other than all symbols and ranges inside this class).
$match = preg_match('/\/[a-zA-Z]-/', "SIP/trkfixo-000072b6");
Im looping over a large number of files in a directory, and want to extract all the numeric values in a filename where it starts lin64exe , for instance, lin64exe005458002.17 would match 005458002.17. I have this part sorted, but in the directory there are other files, such as part005458 and others. How can I make it so I only get the numeric (and . ) after lin64exe ?
This is what I have so far:
[^lin64exe][^OTHERTHINGSHERE$][0-9]+
Regex to match the number with decimal point which was just after to lin64exe is,
^lin64exe\K\d+\.\d+$
DEMO
<?php
$mystring = "lin64exe005458002.17";
$regex = '~^lin64exe\K\d+\.\d+$~';
if (preg_match($regex, $mystring, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> 005458002.17
You can try with look around as well
(?<=^lin64exe)\d+(\.\d+)?$
Here is demo
Pattern explanation:
(?<= look behind to see if there is:
^ the beginning of the string
lin64exe 'lin64exe'
) end of look-behind
\d+ digits (0-9) (1 or more times (most possible))
( group and capture to \1 (optional):
\. '.'
\d+ digits (0-9) (1 or more times (most possible))
)? end of \1
$ the end of the string
Note: use i for ignore case
sample code:
$re = "/(?<=^lin64exe)\\d+(\\.\\d+)?$/i";
$str = "lin64exe005458002.17\nlin64exe005458002\npart005458";
preg_match_all($re, $str, $matches);
You can use this regex and use captured group #1 for your number:
^lin64exe\D*([\d.]+)$
RegEx Demo
Code:
$re = '/^lin64exe\D*([\d.]+)$/i';
$str = "lin64exe005458002.17\npart005458";
if ( preg_match($re, $str, $m) )
var_dump ($m[1]);