Reputation: 1226
I want to detect strings that have a user's age in them, for example :
"I'm 24 years old" "J'ai 25 ans"
So essentially it would look for :
I've used :
/^[0-9]{2} +(ans|year)$/
so far but it only matches very specific strings like "24 year"
Upvotes: 0
Views: 880
Reputation: 27763
Not sure if I have picked the right words, yet you might want to design an expression similar to:
\s+\p{N}{1,3}\s+(?:years?|an(?:née)?s|سنة|سنوات|عاما|साल)
The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.
$re = '/\s+\p{N}{1,3}\s+(?:years?|an(?:née)?s|سنة|سنوات|عاما|साल)/m';
$str = 'I\'m 24 years old
J\'ai 25 ans
I have 25 year
عندي ٢٣ سنة
I\'m 24 years old
मैं 27 साल का हूँ
J\'ai 25 ans
I have 100 year
أنا 27 عاما
عندي ٢٣ سنة';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $match) {
print(trim($match[0]) . "\n");
}
24 years
25 ans
25 year
24 years
27 साल
25 ans
100 year
27 عاما
Upvotes: 0
Reputation: 782785
Get rid of the ^
and $
. They match the beginning and end of the string, so it won't work if you have I am
at the beginning or old
at the end.
If you want to match whole words, use \b
instead.
/\b\d{2} +(ans|years)\b/
And if you want to match numerals other than Arabic, use \d
instead of [0-9]
.
Upvotes: 0
Reputation: 43199
One possible approach might be
\b\p{N}+\s+(?:an|year)s?
which could be used for example in a lookahead. See a demo on regex101.com.
Your initial expression uses anchors, that is your substring can only be matched at the beginning and the end.
Upvotes: 1