I have these strings, and I want to match b=(\d+) only not ab=(\d+). How do I do it?
"ab=10&b=20" -> 20
"b=20&ab=10" -> 20
"b=20" -> 20
"ab=10" -> no match
You could use \b, like:
\bb=(\d+)
Which matches only at a word boundary (between a \w and not a \w).
Use a negative lookbehind:
/(?<!a)b=(\d+)/
Now this will match any number followed by b= if not preceded by the character a.
Test case:
$array = array(
"ab=10&b=20",
"b=20&ab=10",
"b=20",
"ab=10"
);
foreach ($array as $str) {
if (preg_match('/(?<!a)b=(\d+)/', $str, $matches)) {
echo $matches[1], PHP_EOL;
} else {
echo "No match", PHP_EOL;
}
}
Output:
20
20
20
No match
Demo
This is what I got:
(?:[^a-zA-Z])(?:b=(\d+))
(?:[^a-zA-Z]) It can not start with a-Z. You might want to change this, but you get the idea
(?:b=(\d+)) I wrapped it in a group to make the regex combine it, ?: makes sure \\1 will still be 20
Related
I want a regular expression to validate employee ids which are
UC-00001,
UC-00012,
UC-000100. etc
"UC-000" is constant but after that if one digit number exist it becomes UC-00001 and if more den one digit exist then only three zeros needs to be constant.(UC-00010)
I tried using preg_match(/[U]{1}[C]{1}-[0]{3}[0-9]$/) but its not validating properly.
Thanks in advance
You can match three zeroes and then either from 00 to 09 or from 10 up..
^UC-000(?:0\d|[1-9]\d*)$
The pattern matches:
^ Start of string
UC-000 Match literally
(?: Non capture group
0\d Match 0 and a single digit 0-9 (Or use [1-9] to not match 00000)
| Or
[1-9]\d* Match a digit 1-9 and optional digits
) Close non capture group
$ End of string
Regex demo
$strings = [
"UC-00001",
"UC-00012",
"UC-000100",
"UC-00001",
"UC-00010",
"UC-00000",
"UC-000010",
"UC-0000100"
];
$pattern = "~^UC-000(?:0\d|[1-9]\d*)$~";
foreach ($strings as $s) {
if (preg_match($pattern, $s)) {
echo "Match: $s" . PHP_EOL;
} else {
echo "Not match: $s" . PHP_EOL;
}
}
Output
Match: UC-00001
Match: UC-00012
Match: UC-000100
Match: UC-00001
Match: UC-00010
Match: UC-00000
Not match: UC-000010
Not match: UC-0000100
Your pattern is logically correct, save that you didn't allow for multiple digits after the leading 000. I would use this version:
^UC-[0-9]{5,}$
PHP script:
$input = "UC-000100";
if (preg_match("/^UC-[0-9]{5,}$/", $input)) {
echo "VALID";
}
i have the bellow string
$LINE = TCNU1573105 HDPE HTA108 155 155 000893520918 PAL990 25.2750 MT 28.9750 MT
and i want extract the PAL990 from the above string. actually extract PAL990 string or any string that has PAL followed by some digits Like PAL222 or PAL123
i tried many ways and could not get the result. i used,
substr ( $LINE, 77, 3)
but when the value in different position i get the wrong value.
You may use
$LINE = "TCNU1573105 HDPE HTA108 155 155 000893520918 PAL990 25.2750 MT 28.9750 MT";
if (preg_match('~\bPAL\d+\b~', $LINE, $res)) {
echo $res[0]; // => PAL990
}
See the PHP demo and this regex demo.
Details
\b - a word boundary
PAL - a PAL substring
\d+ - 1+ digits
\b - a word boundary.
The preg_match function will return the first match.
Note that in case your string contains similar strings in between hyphens/whitespace you will no longer be able to rely on word boundaries, use custom whitespace boundaries then, i.e.:
'~(?<!\S)PAL\d+(?!\S)~'
See this regex demo
EDIT
If you may have an optional whitespace between PAL and digits, you may use
preg_replace('~.*\b(PAL)\s?(\d+)\b.*~s', '$1$2', $LINE)
See this PHP demo and this regex demo.
Or, match the string you need with spaces, and then remove them:
if (preg_match('~\bPAL ?\d+\b~', $LINE, $res)) {
echo str_replace(" ", "", $res[0]);
}
See yet another PHP demo
Note that ? makes the preceding pattern optional (1 or 0 occurrences are matched).
$string = "123ABC1234 *$%^&abc.";
$newstr = preg_replace('/[^a-zA-Z\']/','',$string);
echo $newstr;
Output:ABCabc
I have to check csv files live and match some expression to get data.
These files can have different type of message so different matching expression.
The message can be something like that
GuiPrinter.ProcessPrint of 116806 25374 K356 S Black Face.png 229 at 1
table
And I want to get 116806 25374 K356 S Black Face.png
. So the regex associate to this kind of file would be something like (GuiPrinter.ProcessPrint of )(.*)([.][png|jpg|jpeg|PNG|JPG|JPEG]*) and I can return $result[2]
But the message and the regex can change, so I need a common function that can return the string that I want based on the regex, the function would have message and regex parameters. Maybe for another file the string that I want would be on first position so my $result[2] won't work.
How can I ensure to always return the string that I want to match ?
Use
\preg_match('/GuiPrinter.ProcessPrint of(.*?)\.(gif|png|bmp|jpe?g)/', $str, $match);
print_r($match[1]);
You could match the text GuiPrinter.ProcessPrint and then use \K to reset the starting point of the reported match.
Match any character zero or more times non greedy .*?, then match a dot \. and any of the image extensions in a non capturing group (?:gif|png|bmp|jpe?g) followed by a word boundary \b
GuiPrinter\.ProcessPrint of \K.*?\.(?:gif|png|bmp|jpe?g)\b
Note that to match the dot literally you have to escape it \.
For example to return 1 match using preg_match:
$str = 'GuiPrinter.ProcessPrint of 116806 25374 K356 S Black Face.png 229 at 1 table';
$re = '/GuiPrinter\.ProcessPrint of \K.*?\.(?:gif|png|bmp|jpe?g)\b/';
function findMatch($message, $regex) {
preg_match($regex, $message, $matches);
return array_shift($matches);
}
$result = findMatch($str, $re);
if ($result) {
echo "Found: $result";
} else {
echo "No match.";
}
Demo
I'm trying to get the string that match with original and with number in the end.
I got these strings:
mod_courts2
mod_courts_config
mod_courts_config2
From these strings I want the one that matches only with "mod_courts" with number in the end.
I'm doing this:
if (strpos($t, "mod_courts") !== FALSE) {
preg_match('/^\w+(\d+)$/U', $t, $match);
echo $match;
}
This returns me "mod_courts2" and "mod_courts_config2", I just want "mod_courts2"
Use the following regex:
/^[a-z]+_[a-z]+(\d+)$/
Explanation:
^ - assert position at the beginning of the string
[a-z]+ - match any alphabet one or more times
_ - match a literal undescore character
[a-z]+ - match any alphabet one or more times
(\d+) - match (and capture) any digit from 0 to 9 one or more times
$ - assert position at the end of the string
Test cases:
$array = array(
'mod_courts2',
'mod_courts_config',
'mod_courts_config2'
);
foreach ($array as $string) {
if(preg_match('/^[a-z]+_[a-z]+(\d+)$/i', $string, $matches)) {
print_r($matches);
}
}
Output:
Array
(
[0] => mod_courts2
[1] => 2
)
Very simply, you can do:
/^(mod_courts\d+)$/
However, if you want exactly the following format: sometext_somettext2, you can use the following regex:
/^([a-zA-Z]+_[a-zA-Z]+\d+)$/
or
/^([^_]+_[^_]+\d+)$/
Demos
http://regex101.com/r/jP8iC1
http://regex101.com/r/tI1uX8
http://regex101.com/r/fX8pO5
^mod_courts\d+$
this should do it
You can just use
^mod_courts[0-9]+$
Meaning mod_courts followed by a number (and only that, thanks to ^$ matching the beginning and end of the string). No need for the strpos check.
Need a regex to get numbers of pages in the thread:
example url : traidnt.net/vb/f25
i tried this :
'~<td class="vbmenu_control" style="font-weight:normal">.*([2-9]{1}|[0-9]{2,}).*</td>~isU'
but it wont work.
Thanking you
/.*?([0-9]{2,}|[2-9]{1}).*/s
matches a one-digit number > 1 or any multi-digit number.
please note that this does not match correctly when: "page 1 of 1 pages"
if the string is fixed, you better go with:
/page \d+ of (\d+) pages/is
or if the string is not absolutely fixed but you want the second number from the string you may use:
/\D*(\d+)\D*(\d+)\D*/s
and use the second sub-match. (will also match correctly when "page 1 of 1 pages".
You can use \d+ to match numbers, and a negative assertion (?!...) to exclude something. Often you need some anchors around to make it work reliably, here word boundaries:
/\b(?!1\b)\d+\b/
This will find all digits, which are not one:
/[^\D1]/
<?php
$str="page 1 of 200 pages";
$matches = array();
if (preg_match('/page (\d+) of (\d+)/', $str, $matches)) {
echo $matches[2];
}
?>
for the updated question:
$url="traidnt.net/vb/f25";
$matches1 = array();
if (preg_match('/f(\d+)/', $url, $matches1)) {
echo $matches1[1];
}
?>