Reputation: 1807
Does someone know tip to duplicate \K feature in Java or Python?
Say you have strings like these: mix of digits and letters, then after the last digit, some letters. We only want to match the last letters here "five". This is only example to explain the feature what I hope to do.
01a01d1101five
In PHP Perl Ruby you have \K you can just do
^[\w]*\d\K[a-z]+$
Only "five" is matched that's great.
In C# you can have variable width lookbehind
(?<=^[\w]*\d)[a-z]+$
Only "five" is matched that's great.
In Java this is what I tried.
If I know min and max width of string behind I can do
(?<=^[\w]{3,9}\d)[a-z]+$
and only "five" is matched.
Actually I don't know the width so my only idea for Java or Python is
^[\w]*\d([a-z]+$)
This matches the whole string and captures "five" in Group#1. So I don't know a tip to match "five" in whole match / Group#0. Do you have one?
Upvotes: 2
Views: 289
Reputation: 60334
You could use a negative lookbehind to get the last letters.
(?<=[0-9])[A-Za-z]+$
Since you are looking for letters that are between the last digit and the end of the string, you don't need to know the length of the digit string preceding the last letters.
If you don't require the digits to be present in the string, you could just use
[A-Za-z]+$
In both of the above, of course, you do use the ^$ match at linebreak option
Upvotes: 1
Reputation: 9644
Random thoughts - in Java, you could use the \G
anchor and search for global matches like so:
^[\w]*\d(?=[a-z]+$)|(?!^)\G[a-z]+$
The first match will eat up the mandatory characters, the second will be the match you want preceded by the correct pattern.
It means the first match is only here to be thrown away (and Python doesn't support the \G
anchor :/), but I don't know of a more general way to do so... I'd be happy to see one.
Upvotes: 1