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
^Foo\s*
with an empty string. Use .{3}
if you need 3 arbitrary characters instead of Foo
.
If you need to use match instead of replace, you need to use a capture group:
^Foo\s*(.*)$
The [^Foo\s?]
is a character class ([...]
) to match any characters except (^
) an F
, a o
, a whitespace (\s
) or a question mark ?
. That's why the F
in Fgh
is also matched. If you want a grouping bracket, use (?:...)
.
^.{3}\s+.*$
should do the trick (it matches 3 arbitrary characters at the beginning of the line, one or more whitespaces and then anything up until the end of the line).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With