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]);
Related
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
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");
String:
lorem ipsum 999
[id:284,286]
[id:28]
Block in brackets may contain a lot of numbers.
Regex:
\[id:(\d+)(,\d+)*\]
What I'd like to see:
284
286
28
Solution using PHP:
preg_match_all('/\[id:(.*)\]/', $input, $ids);
if (strpos($ids[1][0], ',')) {
$ids = explode(',', $ids[1][0]);
foreach ($ids as $id) {
echo $id . "\n";
}
} else {
echo $ids[1][0];
}
But is it possible using regex without explode()?
The explode way is perhaps the best. Unfortunately, PCRE does not remember repeated groups, thus, you either do it in 2 steps (with the explode), or use a \G based regex. Here is a safer regex than the one you are using (if there are no spaces in between the numbers):
$input = "lorem ipsum 999 [id:284,286] [id:28]";
preg_match_all('~\[id:([\d,]*)]~', $input, $ids);
foreach ($ids[1] as $id) {
print_r(explode(',', $id)) . PHP_EOL;
}
See the IDEONE demo
The '~\[id:([\d,]*)]~' regex matches [id: and then matches and captures into Group 1 zero or more (due to * 0+ occurrences quantifier) digits (\d) or ,s.
If you need a one-regex solution, in PHP, if you process individual strings, you can make use of a \G based regex that you can leverage to set up the leading boundary and then match the consecutive numbers:
'~(?:\[id:|(?!^)\G,)\K\d+~'
See the regex demo and this IDEONE demo:
$re = '~(?:\[id:|(?!^)\G,)\K\d+~';
$strs = array("lorem ipsum 999", "[id:284,286]", "[id:28]");
foreach ($strs as $s) {
preg_match_all($re, $s, $matches);
print_r($matches[0]);
}
Pattern details:
(?:\[id:|(?!^)\G,) - match the [id: literal character sequence or the end of each successful match with (?!^)\G with a comma after it
\K - omit the matched value
\d+ - only match 1+ digits
If there can be whitespace between the digits, add \s* after (and perhaps, before) the comma.
Let's say I want to split this string in two variables:
$string = "levis 501";
I will use
preg_match('/\d+/', $string, $num);
preg_match('/\D+/', $string, $text);
but then let's say I want to split this one in two
$string = "levis 5° 501";
as $text = "levis 5°"; and $num = "501";
So my guess is I should add a rule to the preg_match('/\d+/', $string, $num); that looks for numbers only at the END of the string and I want it to be between 2 and 3 digits.
But also the $text match now has one number inside...
How would you do it?
To slit a string in two parts, use any of the following:
preg_match('~^(.*?)\s*(\d+)\D*$~s', $s, $matches);
This regex matches:
^ - the start of the string
(.*?) - Group 1 capturing any one or more characters, as few as possible (as *? is a "lazy" quantifier) up to...
\s* - zero or more whitespace symbols
(\d+) - Group 2 capturing 1 or more digits
\D* - zero or more characters other than digit (it is the opposite shorthand character class to \d)
$ - end of string.
The ~s modifier is a DOTALL one forcing the . to match any character, even a newline, that it does not match without this modifier.
Or
preg_split('~\s*(?=\s*\d+\D*$)~', $s);
This \s*(?=\s*\d+\D*$) pattern:
\s* - zero or more whitespaces, but only if followed by...
(?=\s*\d+\D*$) - zero or more whitespaces followed with 1+ digits followed with 0+ characters other than digits followed with end of string.
The (?=...) construct is a positive lookahead that does not consume characters and just checks if the pattern inside matches and if yes, returns "true", and if not, no match occurs.
See IDEONE demo:
$s = "levis 5° 501";
preg_match('~^(.*?)\s*(\d+)\D*$~s', $s, $matches);
print_r($matches[1] . ": ". $matches[2]. PHP_EOL);
print_r(preg_split('~\s*(?=\s*\d+\D*$)~', $s, 2));
I've been reading up on RegEx docs but I must say I'm still a bit out of my element so I apologize for not posting what I have tried because it was all just plain wrong.
Heres the issue:
I've got images using the following source:
src="http://samplesite/.a/6a015433877b2b970c01a3fd22309b970b-800wi"
I need to get to this:
src="http://newsite.com/wp-content/uploads/2014/07/6a015433877b2b970c01a3fd22309b970b-800wi.jpg"
Essentially removing the /.a/ from the URL and appending a .jpg to the end of the image file name. If it helps in a solution I'm using this plug-in: http://urbangiraffe.com/plugins/search-regex/
Thanks All.
This might help you.
(?<=src="http:\/\/)samplesite\/\.a\/([^"]*)
Online demo
Sample code:
$re = "/(?<=src=\"http:\/\/)samplesite\/\.a\/([^\"]*)/";
$str = "src=\"http://samplesite/.a/6a015433877b2b970c01a3fd22309b970b-800wi\"";
$subst = 'newsite.com/wp-content/uploads/2014/07/$1.jpg';
$result = preg_replace($re, $subst, $str);
Output:
src="http://newsite.com/wp-content/uploads/2014/07/6a015433877b2b970c01a3fd22309b970b-800wi.jpg"
Pattern Description:
(?<= look behind to see if there is:
src="http: 'src="http:'
\/ '/'
\/ '/'
) end of look-behind
samplesite 'samplesite'
\/ '/'
\. '.'
a 'a'
\/ '/'
( group and capture to \1:
[^"]* any character except: '"' (0 or more
times (matching the most amount
possible))
) end of \1
You can try it without using Positive Lookbehind as well
(src="http:\/\/)samplesite\/\.a\/([^"]*)
Online demo
Sample code:
$re = "/(src=\"http:\/\/)samplesite\/\.a\/([^\"]*)/";
$str = "src=\"http://samplesite/.a/6a015433877b2b970c01a3fd22309b970b-800wi\"";
$subst = '$1newsite.com/wp-content/uploads/2014/07/$2.jpg';
$result = preg_replace($re, $subst, $str);
You can use this:
$replaced = preg_replace('~src="http://samplesite/\.a/([^"]+)"~',
'src="http://newsite.com/wp-content/uploads/2014/07/\1.jpg"',
$yourstring);
Explanation
([^"]+) matches any characters that are not a " to Group 1
\1 inserts Group 1 in the replacement.