I’m sure there’s a simple solution to this but it’s got me stuck so hopefully someone can give me a hand. I’ve got multiple lines of input that look like this:
FooABC
Foo XYZ123
FooFgh
I need a regex (in Java) that will give me:
ABC
XYZ123
Fgh
The best I’ve come up with so far is:
[^Foo\s?].*$
Which I thought was working until I noticed that it was removing the capital F from Fgh. Is there any way I can make it exactly ignore the first three characters and one or more whitespace characters then just capture everything else?
EDIT / CLARIFICATION
I should have said that I can’t use substring / replace or any of the things I would normally do in this situation because this regex will be fed into deployed code that can’t currently be updated. What the code is doing is this:
Pattern pattern = Pattern.compile( regex );
Matcher matcher = pattern.matcher( data );
matcher.find();
String extracted = matcher.group();
and it’s then using the extracted string as a key for a look up. So it has to extract the key in one pass.
Replace
with an empty string. Use
.{3}if you need 3 arbitrary characters instead ofFoo.If you need to use match instead of replace, you need to use a capture group:
The
[^Foo\s?]is a character class ([...]) to match any characters except (^) anF, ao, a whitespace (\s) or a question mark?. That’s why theFinFghis also matched. If you want a grouping bracket, use(?:...).