Getting back the right amount of matches
Getting back the right amount of matches
I have a simple problem, but I've had a hard time find a simple and effective solution to it.
Since I didn't have any success with way I posted what I need to solve the first and the second times, I am going to try it a third time with a more direct question.
After searching the web, I found a solution to dealing with German and French characters, but the underlying problem with the matches I get is the same.
I simplified the script, so that anyone can try it out.
<?php
$lines=array("Ich weiß wirklich nicht, womit er prahlt!: I really
don't know what he's bragging of!","Worüber hat er gesprochen?: what
did he talked about?");
foreach($lines as $line){
preg_match_all('/b([A-Za-zäöüÖÄÜßs.,'!?])+([A-Za-
zs.;'-!?]+)/',$line,$lines1,PREG_PATTERN_ORDER);
echo 'results = '.$lines1[0][0].'<BR>';
}
?>
From preg_match_all I only get two matches:
results = Ich wei� wirklich nicht, womit er prahlt!
results = Wor�ber hat er gesprochen?
But from https://regexr.com/ and https://regex101.com/ I get four matches with the same regular expression.
Ich weiß wirklich nicht, womit er prahlt!
I really don't know what he's bragging of!
Worüber hat er gesprochen?
what did he talked about?
Does anyone know why there's this discrepancy? How can I get four matches with preg_match_all instead of only two?
Thanks in advance.
- er macht | er machte | er hat gemacht;
Well don't know what you want and you aren't commenting so can't help. The
wei? issues sounds like you split the unicode character into individual bytes, maybe add u modifier.– user3783243
Jul 1 at 12:29
wei?
u
@ user3783243 - Thanks for the tip, but am I suposed to add the u modifier to the preg_match_all function? I tried to explain what I want again. Thanks
– Bert_AT
Jul 1 at 19:16
Yes, that is clearer. In your character class the
a-zA-Z won't expand to the german characters. Replace that with p{L}.– user3783243
Jul 1 at 19:26
a-zA-Z
p{L}
@user3783243 - Any ideas about the rest? How can I mark your answer as useful without saying that my problem has been solved? Thanks. I do hope someone can shed some light here. :)
– Bert_AT
Jul 1 at 19:53
1 Answer
1
If the strings are all separated by the ":" it's really no need to match by the characters. Use your regex to grab the left side and the right side of the ":" regarding of what they are. This works fine on the test-strings you've provided.
$lines=array(
"Ich weiß wirklich nicht, womit er prahlt!: I really don't know what he's bragging of!",
"Worüber hat er gesprochen?: what did he talked about?"
);
$allItems=array();
$pattern = '/(.*?):s?(.*?$)/s';
foreach($lines as $line){
preg_match_all($pattern, $line, $result, PREG_PATTERN_ORDER);
array_push($allItems, $result[1][0], $result[2][0]);
}
print_r($allItems);
OUTPUT
Array ( [0] => Ich weiß wirklich nicht, womit er prahlt! [1] => I really don't know what he's bragging of! [2] => Worüber hat er gesprochen? [3] => what did he talked about? )
EDIT
For this specific scenario you could omit the use of regex alltogether by instead simply using this, the resulting output would be the same:
$lines=array(
"Ich weiß wirklich nicht, womit er prahlt!: I really don't know what he's bragging of!",
"Worüber hat er gesprochen?: what did he talked about?"
);
$allItems=array();
foreach($lines as $line){
$values = explode(": ", $line);
array_push($allItems, $values[0], $values[1]);
}
print_r($allItems);
I'd prefer to go with this last, but you also said you had other types of strings so it'd probably be best in that case to keep the regex-way if the other string types would just need a pattern-modification to work.
I've wouldn't expect that there is only one space after colon, maybe update regex to '/([^:]*):s*(.*?$)/s',.. same for explode, maybe array_map('trim', explode(':', $line);
– 2oppin
2 days ago
@Colobri - BEAUTIFUL!!! That's exactly what I needed from the beginning. Simple, elegant, and effective!! Now I need to learn how to say my question has been answer, and also thank 2oppin for that beautiful yet simple RegEx that he provided. It's very useful. Next I'm going to add part of my final code in case anyone would be interested. Colibri - You mentioned that you'd prefer to use the explode() instead, what's better?
– Bert_AT
yesterday
@Bert_AT - Glad I could help you! :D - To mark an answer as "the" answer, you click the checkmark beneath the rank-buttons. :) In this case, I would go with 'explode' if I knew that all strings would be in this format. I don't know the impact on performance, but I find it more 'readable' than the regex. Regex can be used to do a lot of really advanced stuff, and it's super-fun - but it feels a bit overkill for this specific scenario. :-)
– Robin
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
- er macht | er machte | er hat gemacht;doesn't have a colon.– user3783243
Jul 1 at 11:48