let's go example:
"blablabla. name john , i'm 21 years old. blablabla"
other example:
"blablabla. name john , i'm 21 years old. - hi i'm mary , i'm 22 years old."
basically, want match age of first person (here, 21, 23 or whatever). idea know i'll have sentence beginning "my name $name , i'm 21" can't afford know $name. gross idea select number after "my name "+something+" , i'm ".
how 1 regex, knowing can't use catch groups?
what have far:
(?<=<my name )(.*)(?= years old) ideally work:
(?<=<my name .* , i'm )(.*)(?= years old) ... not! .* can't in ahead group apparently (which makes sense).
thank kindly.
/my name (\w+) , i'm (\d+) years old./ now first matched group name, second matched group age.
if reason don't want use groups, can match:
/(?<=my name )\w+(?= , i'm )/ for name and:
/(?<= , i'm )\d+(?= years old.)/ for age.
have noticed, lookbehinds variable length not allowed (at least in regex engines know of, not logically impossible). however, can use
\k alternative: /my name \w+ , i'm \k\d+(?= years old.)/
Comments
Post a Comment