>I'm not sure what terms to look for here. I want to use a regex that will
>match as little as possible in a string from the *end* of the string.
>>I'm not sure what terms to look for here. I want to use a regex that will
>>match as little as possible in a string from the *end* of the string.
[quoted text clipped - 10 lines]
> It might help to try to express what you want in English,
> in a manner that does not need examples to be understood.
Okay.
I want to be able to specify a phrase with a regex and remove from the last
occurrence of that phrase to the end of the original string.
Examples:
Full String: "The date 2008-01-07 is one day before Elvis' birthday on
2008-01-08, which is tomorrow."
Match: "[0-9]{2,4}-[0-9]{1,2}-[0-9]{1,2}"
Desired Result: "The date 2008-01-07 is one day before Elvis' birthday on "
It matches the LAST date that fits the format and removes from the last
match on. If I used "[0-9]{2,4}-[0-9]{1,2}-[0-9]{1,2}.*?" it'll match from
the first date on.
Full String: "One Two Three Two One"
Match: "Two"
Desired Result: "One Two Three "
I've tried using negative lookaheads, but, as best I can guess, in that last
example, if I use any kind of quantifier, then the first match I get
is "Two Three Two One" and it seems to not see it can match just "Two One".
I have used Match.find() and a loop to get the last position of the match,
then used Match.start() to get the position, then get a substring of the
original string, using that start position as where the substring ends, but
if I want to delete from just after the match, then I have to check to make
sure I'm not out of bounds and so on. I would think there would be an easy
way to match the last occurrence of a phrase to the end of a string instead
of having to loop through it.
Thanks!
Hal
Robert Klemme - 07 Jan 2008 18:09 GMT
>>> I'm not sure what terms to look for here. I want to use a regex that will
>>> match as little as possible in a string from the *end* of the string.
[quoted text clipped - 40 lines]
> way to match the last occurrence of a phrase to the end of a string instead
> of having to loop through it.
In your case you can use this
http://java.sun.com/javase/6/docs/api/java/lang/String.html#lastIndexOf(java.lan
g.String)
robert
Hal Vaughan - 07 Jan 2008 18:17 GMT
>>>> I'm not sure what terms to look for here. I want to use a regex that
>>>> will match as little as possible in a string from the *end* of the
[quoted text clipped - 46 lines]
>
> In your case you can use this
http://java.sun.com/javase/6/docs/api/java/lang/String.html#lastIndexOf(java.lan
g.String)
But that uses a String not a regex. I tried it.
Thanks, though.
Hal
Stefan Ram - 07 Jan 2008 18:12 GMT
>I want to be able to specify a phrase with a regex and remove from the last
>occurrence of that phrase to the end of the original string.
Thus, for the phrase »alpha« and the text
»alpha beta alpha gamma alpha delta alpha epsilon«,
the text to be removed is »alpha epsilon«.
public class Main
{
public static void test( final java.lang.String text )
{
final java.util.regex.Matcher matcher =
java.util.regex.Pattern.compile
( "^(.*)alpha.*$" ).
matcher( text );
while( matcher.find() )
java.lang.System.out.println( matcher.group( 1 )); }
public static void main( final java.lang.String[] args )
{ test
( "alpha beta alpha gamma alpha delta alpha epsilon alpha zeta" ); }}
alpha beta alpha gamma alpha delta alpha epsilon
Hal Vaughan - 07 Jan 2008 18:24 GMT
>>I want to be able to specify a phrase with a regex and remove from the
>>last occurrence of that phrase to the end of the original string.
[quoted text clipped - 20 lines]
>
> alpha beta alpha gamma alpha delta alpha epsilon
Then what you're doing, in essence, is using a greedy quantifier at the
start to gobble up as much as possible so it only finds the last occurrence
and then just using capture to get the text that's to be kept and using
that as the replacement.
Am I right in how this is working? I see it works, I just want to be sure I
understand it clearly.
Thanks!
Hal
Stefan Ram - 07 Jan 2008 18:31 GMT
>Then what you're doing, in essence, is using a greedy
>quantifier at the start to gobble up as much as possible so it
>only finds the last occurrence and then just using capture to
>get the text that's to be kept and using that as the
>replacement.
I believe so.
Hal Vaughan - 07 Jan 2008 18:34 GMT
>>Then what you're doing, in essence, is using a greedy
>>quantifier at the start to gobble up as much as possible so it
[quoted text clipped - 3 lines]
>
> I believe so.
Okay. I got it.
Thanks!
Hal
Stefan Ram - 07 Jan 2008 19:49 GMT
>Okay. I got it.
One still might ask, whether there is a way to just inspect
the end of the string. I am not absolutely sure, whether the
following code really does that, but I would try it this way:
public class Main
{
public static java.lang.String pos( final java.lang.String text )
{
final java.util.regex.Matcher matcher =
java.util.regex.Pattern.compile
( "(?<=(alpha.{0,2147483642}?)$)" ).
matcher( text );
return matcher.find() ? matcher.group( 1 ) : ""; }
public static void main( final java.lang.String[] args )
{
final java.lang.String source = "alpha beta alpha gamma alpha delta";
final java.lang.String stringToBeRemoved = pos( source );
java.lang.System.out.println( stringToBeRemoved );
final java.lang.String result = source.substring
( 0, source.length() - stringToBeRemoved.length() );
java.lang.System.out.println( result ); }}
alpha delta
alpha beta alpha gamma