Reputation: 3311
I'm trying to create regex to retrieve last number if there was a number or any number if there wasn't any from a string. Examples:
6 łyżek stopionego masła -> 6
5 łyżek blabla, 6 łyżek masła -> 6
5 łyżek mąki lub masła -> 5
I'm matching only on masła (changing variable) so it has to be included in regex
EDIT: I cannot explain what I actually need: Here is regex101 example: https://regex101.com/r/pEeRk3/1
EDIT2: Emma's solution works great, but I would need to parse decimals and 2multiple digit numbers as well, meaning that those would match as well: https://regex101.com/r/pEeRk3/3 - I added examples with answers in the link
Upvotes: 1
Views: 55
Reputation: 163632
If you want to match the last occurence of a digit with a decimal and you word has to follow this value, you might use lookarounds:
(?<!\S)\d+(?:\.\d+)?(?!\S)(?!.*\d)(?=.*masła)
(?<!\S)\d+(?:\.\d+)?(?!\S)
Match 1+ digits with an optional past to match a dot and 1+ digits(?!.*\d)
assert that there are no more digits following(?=.*masła)
Assert what is on the right is your wordOr you might use a capturing group:
(?<!\S)(\d+(?:\.\d+)?)[^\d\n]* masła(?!\S)[^\d\n]*$
Upvotes: 2
Reputation: 27743
This expression might simply suffice:
.*([0-9])
if we are interested in one digit only, or
.*([0-9]+)
if multiple digits might be desired.
If those strings with masła
are desired, we can expand our expression to:
(?=.*masła).*([0-9])
If we would not be validating our numbers and our number would be valid, with commas or dots, then this expression might likely return our desired output:
(?=.*masła)([0-9,.]+)(\D*)$
Upvotes: 1