This is my code:
preg_match('/(?<=groups\\/)\\d+/im', $_SERVER['REQUEST_URI'], $matches)
When $_SERVER['REQUEST_URI'] = "/groups/S14022/", $matches is empty.
When $_SERVER['REQUEST_URI'] = "/groups/S14022", $matches[0] gets me "S14022".
What do I have to change on the regex to match both cases?
Try this way,
$re = "/(?<=\\/groups\\/)S\\d+\\/?/m";
$str = "/groups/S14022/\n/groups/S14022";
preg_match_all($re, $str, $matches);
See Demo https://regex101.com/r/qP2rO5/1
I think all you need is this regex (as in your regex, you are missing S):
(?<=groups\/)S\d+
PHP code:
$re = "/(?<=groups\\/)S\\d+/im";
$str = "/groups/S14022/\n/groups/S14022";
preg_match_all($re, $str, $matches);
print_r($matches);
Output of the sample program:
Array
(
[0] => Array
(
[0] => S14022
[1] => S14022
)
)
Related
I have a string:
href="javascript:KUBalloonOpen('0','http://img13.shop-pro.jp/PA01069/800/product/93163228.jpg',
I want to extract the URL from the above. I tried using this expression:
$regex = "KUBalloonOpen('0','(.*)',";
But it does not work. What is the correct regexp?
You need to escape single first ( your regex:
$str ="href=\"javascript:KUBalloonOpen('0','http://img13.shop-pro.jp/PA01069/800/product/93163228.jpg',";
$regex = "/KUBalloonOpen\('0','(.*)\',/";
preg_match($regex, $str, $match);
$match returns:
Array (
[0] => KUBalloonOpen('0','http://img13.shop-pro.jp/PA01069/800/product/93163228.jpg',
[1] => http://img13.shop-pro.jp/PA01069/800/product/93163228.jpg
)
I need to extract a predefined set of hashtags from a blob of text, then extract what number follows right after it if any. Eg. I'd need to extract 30 from "Test string with #other30 hashtag". I assumed preg_match_all would be the right choice.
Some test code:
$hashtag = '#other';
$string = 'Test string with #other30 hashtag';
$matches = [];
preg_match_all('/' . $hashtag . '\d*/', $string, $matches);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => #other30
)
)
Perfect... Works as expected. Now to extract the number:
$string = $matches[0][0]; // #other30
$matches = [];
preg_match_all('/\d*/', $string, $matches);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] => 30
[7] =>
)
)
What? Looks like it's trying to match every character?
I'm aware of some preg_match_all related answers (one, two), but they all use a parenthesized subpattern. According to documentation - it is optional.
What am I missing? How do I simply get all matches into an array that match such a basic regex like /\d*/ There doesn't seem to be a more appropriate function in php for that.
I never thought I'd be scratching my head with such a basic thing in PHP. Much appreciated.
You need to replace:
preg_match_all('/\d*/', $string, $matches);
with:
preg_match_all('/\d+/', $string, $matches);
Replace * with +
Because
* Match zero or more times.
+ Match one or more times.
You can use a capturing group:
preg_match_all('/' . $hashtag . '(\d*)/', $string, $matches);
echo $matches[1][0] . "\n";
//=> 30
Here (\d*) will capture the number after $hashtag.
Also see, that you can reset after a certain point to get part of a match by using \K. And of course need to use \d+ instead of \d* to match one or more digits. Else there would be matches in gaps in between the characters where zero or more digits matches.
So your code can be reduced to
$hashtag = '#other';
$string = 'Test string with #other30 #other31 hashtag';
preg_match_all('/' . $hashtag . '\K\d+/', $string, $matches);
print_r($matches[0]);
See the demo at eval.in and consider using preg_quote for $hashtag.
PHP Fiddle
<?php
$hashtag = '#other';
$string = 'Test string with #other30 hashtag';
$matches = [];
preg_match_all('/' . $hashtag . '\d*/', $string, $matches);
$string = preg_match_all('#\d+#', $matches[0][0], $m);
echo $m[0][0];
?>
How can I get only the Name/Variable which is "regexed"? Like in this case the $1 or $0 in the anchor's href?
When I try to echo the $1 or $0 I get a Syntax Error because it's a Number.
At the Moment the $str is a whole Text.
function convertHashtags($str){
$regex = "/#+([a-zA-Z0-9_]+)/";
$str = preg_replace($regex, '$0', $str);
return($str);
}
Simple use preg_match before preg_replace, eg
preg_match($regex, $str, $matches);
Assuming the pattern actually matched, you should have the results in $matches[0] and $matches[1] which are the equivalent of $0 and $1 in the replace string.
FYI, the $n tokens in the replacement string are not variables though I can see how that can be confusing. They are simply references to matched groups (or the entire match in the case of $0) in the regex.
See http://php.net/manual/function.preg-replace.php#refsect1-function.preg-replace-parameters
To find multiple matches in $str, use preg_match_all(). It's almost the same only it populates $matches with a collection of matches. Use the PREG_SET_ORDER flag as the 4th argument to make the array workable. For example...
$str = ' xD #lol and #testing';
$regex = '/#(\w+)/';
preg_match_all($regex, $str, $allMatches, PREG_SET_ORDER);
print_r($allMatches);
produces...
Array
(
[0] => Array
(
[0] => #lol
[1] => lol
)
[1] => Array
(
[0] => #testing
[1] => testing
)
)
How do I get an array of usernames from a string tagged like in Twitter with the '#' prefix using regex or similar?
For example:
Input:
hello #person my name is #joebloggs
Output (array):
['person', 'joebloggs']
Another solution
#[^\s]+
Usage:
$string = 'hello #person my name is #joebloggs';
$pattern = '/#[^\s]+/';
preg_match_all($pattern, $string, $matches);
print_r($matches[0]);
Output:
Array
(
[0] => #person
[1] => #joebloggs
)
Do this:
$regex = '~#\K\S+~';
preg_match_all($regex, $yourstring, $matches);
print_r($matches[0]);
See the matches in the Regex Demo.
Explanation
# matches the AT (but it will not be returned)
The \K tells the engine to drop what was matched so far from the final match it returns
\S+ matches any non-space characters
use this :
<?php
$re = "/(?<=#)[^\s]+/";
$str = "asdasd asda 232 #asdasd sd232 soi #other asdnasda asjdajh #asdasd";
preg_match_all($re, $str, $matches);
print_r($matches);
demo here : https://eval.in/173103
output:
Array
(
[0] => Array
(
[0] => asdasd
[1] => other
[2] => asdasd
)
)
What's the right pattern to obtain something like that using preg_split.
Input:
Src.[VALUE1] + abs(Src.[VALUE2])
Output:
Array (
[0] => Src.[VALUE1]
[1] => Src.[VALUE2]
)
Instead of using preg_split, using preg_match_all makes more sense in this case:
preg_match_all('/\w+\.\[\w+\]/', $str, $matches);
$matches = $matches[0];
Result of $matches:
Array
(
[0] => Src.[VALUE1]
[1] => Src.[VALUE2]
)
This regex should be fine
Src\.\[[^\]]+\]
But instead of preg_split I'd suggest using preg_match_all
$string = 'Src.[VALUE1] + abs(Src.[VALUE2])';
$matches = array();
preg_match_all('/Src\.\[[^\]]+\]/', $string, $matches);
All matches you're looking for will be bound to $matches[0] array.
I guess preg_match_all is what you want. This works -
$string = "Src.[VALUE1] + abs(Src.[VALUE2])";
$regex = "/Src\.\[.*?\]/";
preg_match_all($regex, $string, $matches);
var_dump($matches[0]);
/*
OUTPUT
*/
array
0 => string 'Src.[VALUE1]' (length=12)
1 => string 'Src.[VALUE2]' (length=12)