$string = 'Foo (Bar) (Baz)'
preg_match('#\((.*?)\)#', $string, $match);
In the above PHP, $match is returned as
array ( 0 => '(Bar)', 1 => 'Bar', )
Is it possible to alter the regex so it returns:
array ( 0 => '(Baz)', 1 => 'Baz', )
.i.e. the final word in parenthesis.
Thanks.
#.*\((.*?)\)#
try this.this should do it.
or
#\((.*?)\)(?!.*\()#
You can use this regex which uses lookaheads and behinds to make it more clear
/(?<=\()(.*?)(?=\($)/
This is what I would do:
\(((?!.*\().*?)\)
Debuggex Demo
Related
I need to split this format of strings CF12:10 into array like below,
[0] => CF, [1] => 12, [2] => 10
Numbers and String of the provided string can be any length. I have found the php preg_match function but don't know how to make regular expression for my case. Any solution would be highly appreciated.
You could use this regex to match the individual parts:
^(\D+)(\d+):(.*)$
It matches start of string, some number of non-digit characters (\D+), followed by some number of digits (\d+), a colon and some number of characters after the : and before end-of-line. In PHP you can use preg_match to then find all the matching groups:
$input = 'CF12:10';
preg_match('/^(\D+)(\d+):(.*)$/', $input, $matches);
array_shift($matches);
print_r($matches);
Output:
Array
(
[0] => CF
[1] => 12
[2] => 10
)
Demo on 3v4l.org
Try the following code if it helps you
$str = 'C12:10';
$arr = preg_match('~^(.*?)(\d+):(.*)~m', $str, $matches);
array_shift($matches);
echo '<pre>';print_r($matches);
I have the following string url:
HostName=MyHostName;SharedAccessKeyName=SOMETHING;SharedAccessKey=VALUE+VALUE=
I need to extract the key-value pair in an array. I have used parse_str() in PHP below is my code:
<?php
$arr = array();
$str = "HostName=MyHostName&SharedAccessKeyName=SOMETHING&SharedAccessKey=VALUE+VALUE=";
parse_str($str,$arr);
var_dump($arr);
output:
array (
'HostName' => 'MyHostName',
'SharedAccessKeyName' => 'SOMETHING',
'SharedAccessKey' => 'VALUE VALUE=',
)
you can see in the SharedAccessKey char + is replaced by space for this issue, I referred the Similiar Question, Marked answer is not the correct one according to the OP scenario, This says that first do urlencode() and then pass it because parse_str() first decode URL then separate the key-values but this will return array object of a single array which return the whole string as it is like for my case its output is like:
Array
(
[HostName=MyHostName&SharedAccessKeyName=SOMETHING&SharedAccessKey=VALUE+VALUE=] =>
)
Please help me out, not for only + char rather for all the characters should come same as they by the parse_str()
You could try emulating parse_str with preg_match_all:
preg_match_all('/(?:^|\G)(\w+)=([^&]+)(?:&|$)/', $str, $matches);
print_r(array_combine($matches[1], $matches[2]));
Output:
Array (
[HostName] => MyHostName
[SharedAccessKeyName] => SOMETHING
[SharedAccessKey] => VALUE+VALUE=
)
Demo on 3v4l.org
$str = "HostName=MyHostName&SharedAccessKeyName=SOMETHING&SharedAccessKey=VALUE+VALUE=";
preg_match_all('/(?:^|G)(w+)=([^&]+)(?:&|$)/', $str, $matches);
print_r(array_combine($matches[1], $matches[2]));
I have a regex that tries to match for 2 or more words, but it isn't working as it's suppose to. What am I doing wrong?
$string = "i dont know , do you know?";
preg_match("~([a-z']+\b){2,}~", $string, $match);
echo "<pre>";
print_r($match);
echo "</pre>";
Expected Result:
Array ( i dont know )
Actual Result:
Array ( )
This will match for string that contains exactly 2 words or more:
/([a-zA-Z]+\s?\b){2,}/g you can go http://www.regexr.com/ and test it
PHP:
$string = "i dont know , do you know?";
preg_match("/([a-zA-Z]+\s?\b){2,}/", $string, $match);
echo "<pre>";
print_r($match);
echo "</pre>";
Note: do not use the /g in the PHP code
This one should work: ~([\w']+(\s+|[^\w\s])){2,}~g, which also match string like "I do!"
Test it here
I think you are missing how the {} are used, to match two words
preg_match_all('/([a-z]+)/i', 'one two', $match );
if( $match && count($match[1]) > 1 ){
....
}
Match is
array (
0 =>
array (
0 => 'one',
1 => 'two',
),
1 =>
array (
0 => 'one',
1 => 'two',
),
)
Match will have all matches of the pattern, so then its trivial to just count them up...
When using
preg_match('/(\w+){2,}/', 'one two', $match );
Match is
array (
0 => 'one',
1 => 'e',
)
clearly not what you want.
The only way I see with preg_match is with this /([a-z]+\s+[a-z]+)/
preg_match ([a-z']+\b){2,} http://www.phpliveregex.com/p/frM
preg_match ([a-z]+\s+[a-z]+) http://www.phpliveregex.com/p/frO
Suggested
preg_match_all ([a-z]+) http://www.phpliveregex.com/p/frR ( may have to select preg_match_all on the site )
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)
I have a string like this
ch:keyword
ch:test
ch:some_text
I need a regular expression which will match all of the strings, however, it must not match the following:
ch: (ch: is proceeded by a space, or any number of spaces)
ch: (ch: is proceeded by nothing)
I am able to deduce the length of the string with the 'ch:' in it.
Any help would be appreciated; I am using PHP's preg_match()
Edit: I have tried this:
preg_match("/^ch:[A-Za-z_0-9]/", $str, $matches)
However, this only matches 1 character after the string. I tried putting a * after the closing square bracket, but this matches spaces, which I don't want.
preg_match('/^ch:(\S+)/', $string, $matches);
print_r($matches);
\S+ is for matching 1 or more non-space characters. This should work for you.
Try this regular expression:
^ch:\S.*$
$str = <<<TEXT
ch:keyword
ch:test
ch:
ch:some_text
ch: red
TEXT;
preg_match_all('|ch\:(\S+)|', $str, $matches);
echo '<pre>'; print_r($matches); echo '</pre>';
Output:
Array
(
[0] => Array
(
[0] => ch:keyword
[1] => ch:test
[2] => ch:some_text
)
[1] => Array
(
[0] => keyword
[1] => test
[2] => some_text
)
)
Try using this:
preg_match('/(?<! +)ch:[^ ].*/', $str);