Reputation: 3105
I have a string
Dažikliai. Nepavojingas. Savybės: Geltonas ekstrahuojamas iš kurkumos, neapalo šaknies. Arba gaunamas sintezės būdu.
and I want to match a word after static string Savybės
. until first dot.
So my output should be:
Geltonas ekstrahuojamas iš kurkumos, neapalo šaknies
Here what I try so far
"/^Savybės=*$/"
but this doesn't give any results.
Upvotes: 1
Views: 1522
Reputation: 627419
You do not need a lookbehind and lazy dot matching that are inefficient. You can just use a capturing group and a negated character class to match any character but a dot:
$re = '/\bSavybės:\s*([^.]*)/u';
$str = "Dažikliai. Nepavojingas. Savybės: Geltonas ekstrahuojamas iš kurkumos, neapalo šaknies. Arba gaunamas sintezės būdu.";
preg_match($re, $str, $matches);
echo $matches[1];
See IDEONE demo (note the /u
modifier, too)
The regex matches:
\b
- word boundary (so as not to match any word that contains "Savybės")Savybės:
- literal Savybės:
character sequence \s*
- 0 or more whitespace([^.]*)
- capture group #1 that will hold our value matching 0 or more characters other than a literal .
It takes my regex 14 steps to return the valid match, and it takes (?<=Savybės:\s).*?(?=\.)
179 steps.
Upvotes: 2
Reputation: 241198
You could use a positive lookbehind:
(?<=Savybės:\s).*?(?=\.)
Input:
Dažikliai. Nepavojingas. Savybės: Geltonas ekstrahuojamas iš kurkumos, neapalo šaknies. Arba gaunamas sintezės būdu.
Matches:
Geltonas ekstrahuojamas iš kurkumos, neapalo šaknies
If you want to include the .
character at the end:
(?<=Savybės:\s).*?\.
Input:
Dažikliai. Nepavojingas. Savybės: Geltonas ekstrahuojamas iš kurkumos, neapalo šaknies. Arba gaunamas sintezės būdu.
Matches:
Geltonas ekstrahuojamas iš kurkumos, neapalo šaknies.
Upvotes: 3