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 )
Related
This question already has answers here:
preg match count matches
(5 answers)
Closed 6 years ago.
I have a string like this:
$str = 'this is a string';
And this is my pattern: /i/g. There is three occurrence (as you see in the string above, it is containing three i). Now I need to count that. How can I get that number?
you can use substr_count() as well as preg_match_all()
echo substr_count("this is a string", "i"); // will echo 3
echo $k_count = preg_match_all('/i/i', 'this is a string', $out); // will echo 3
other method is convert into array and then count it:
$arr = str_split('this is a string');
$counts = array_count_values($arr);
print_r($counts);
output:
Array
(
[t] => 2
[h] => 1
[i] => 3
[s] => 3
[ ] => 3
[a] => 1
[r] => 1
[n] => 1
[g] => 1
)
You should use substr_count().
$str = 'this is a string';
echo substr_count($str, "i"); // 3
You can also use mb_substr_count()
$str = 'this is a string';
echo mb_substr_count($str, "i"); // 3
substr_count — Count the number of substring occurrences
mb_substr_count — Count the number of substring occurrences
preg_match_all could be a better fit.
Here is an example:
<?php
$subject = "a test string a";
$pattern = '/a/i';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
?>
Prints:
Array ( [0] => Array ( [0] => a [1] => a ) )
I have a variable (text) and it is updated with sentences every time there is an update. When i display this array it turns into 1 long sentence, and i want to break this up in single sentences for readability.
<?php
$pattern = '~\\d+-\\d+-\\d{4} // \\w+: ~ ';
$subject = '01-02-2015 // john: info text goes here 10-12-2015 // peter: some more info
';
$matches = array();
$result = preg_match_all ($pattern, $subject, $matches);
?>
Which gives this output:
$matches:
array (
0 =>
array (
0 => '01-02-2015 // john: ',
1 => '10-12-2015 // peter: ',
),
)
I'd like the output to be:
$matches:
array (
0 =>
array (
0 => '01-02-2015 // john: info text goes here',
1 => '10-12-2015 // peter: some more info',
),
)
I need the output to be like this so i can use a foreach loop to print each sentence.
ps. I'd like to try to get it to work this way first, because otherwise i'd need to change a lot of entries in the database.
pps. I'm also not a hero with regex as you can see, so i hope someone can help me out!
Just change your regex like below,
$pattern = '~\d+-\d+-\d{4} // \w+: .*?(?=\s\d+|$)~';
.*? will do a non-greedy match of zero or more characters until a space followed by digits or end of the line is reached.
DEMO
$str = "01-02-2015 // john: info text goes here 10-12-2015 // peter: some more info";
preg_match_all('~\d+-\d+-\d{4} // \w+: .*?(?=\s\d+|$)~', $str, $matches);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => 01-02-2015 // john: info text goes here
[1] => 10-12-2015 // peter: some more info
)
)
$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
I would like to split a string like this:
Mystreetway 123, 1.th
So that I can have the following output array:
0 => Mystreetway
1 => 123, 1.th
The code must split the string before the first integer found. The substring from the first integer to the end of the string should become the second element upon splitting
I have tried the following found solution:
$key = "Mystreetway 123, 1.th";
$pattern = "/(\d+)/";
$array = preg_split($pattern, $key, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($array);
But it returns the following:
[0] => Mystreetway [1] => 123 [2] => , [3] => 1 [4] => .th
Split with a lookahead pattern instead (so you won't have to juggle captured delimiters), then limit the number of groups to just two (with third param of preg_split:
$key= "Mystreetway 123, 1.th";
$pattern = '/(?=\d)/';
$array = preg_split($pattern, $key, 2);
print_r($array);
Use a lookahead assertion to match a zero-width (empty) expression before a digit:
/(?=\d)/
The parameter you're looking for is limit. Change your third parameter from -1 (meaning, split as many times as necessary) to 1 (meaning, split only once) and the string will be split on the first integer.
$key= "Mystreetway 123, 1.th";
$pattern = "/(\d+)/";
$array = preg_split($pattern, $key, 1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($array);
Edit: For your case, this doesn't actually work, but if we change your regular expression slightly it works as desired;
$key= "Mystreetway 123, 1.th";
$pattern = "/(\d.*)/"; // Note we're now looking for a digit followed by anything
$array = preg_split($pattern, $key, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($array);
Output:
Array
(
[0] => Mystreetway
[1] => 123, 1.th
)
Using preg_match
preg_match('/(\D*)(\d*.*)/', $input_line, $output_array);
Output
array(3
0 => Mystreetway 123, 1.th
1 => Mystreetway
2 => 123, 1.th
)
For the cleanest output, with no lookaround, match all leading non-digit characters, then forget them with \K, then consume the unneeded space character before the first occurring digit. Limit the explosions to only produce 2 elements.
Code: (Demo)
$string = "Mystreetway 123, 1.th";
var_export (
preg_split('/\D+\K /', $string, 2)
);
Output:
array (
0 => 'Mystreetway',
1 => '123, 1.th',
)
I'm creating regular expression in the form: A | B | C ... automatically, by program,
where A, B, C, ... are constant strings.
I need to find all the matches that correspond to these regular expression,
even if the A, B, C, ... have not empty intersection, or someone is substring of other.
Example:
preg_match_all ('/Hello World|Hello|World lo/i', 'xxxHello worldxxx', $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
var_export ($m);
It gives:
array (
0 =>
array (
0 =>
array (
0 => 'Hello World'
1 => 3, // start of match
)
)
)
I would need:
array (
0 =>
array (
0 =>
array (
0 => 'Hello World'
1 => 3, // start of match
)
1 =>
array (
0 => 'Hello'
1 => 3, // start of match
)
2 =>
array (
0 => 'lo world'
1 => 6, // start of match
)
)
)
Is there any way to get it?
Thanks
Run a preg_match_all for each expression.
I would use strpos:
$str = 'xxxHello worldxxx';
$arr = array('Hello World', 'Hello', 'World');
foreach($arr as $word) {
$pos = strpos(strtolower($str), strtolower($word));
echo "$word found at char $pos\n";
}
output:
Hello World found at char 3
Hello found at char 3
World found at char 9