Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex Everything Before and Including Match

Tags:

java

regex

I need the regex expression to remove any text before a match and including the match

eg. I want to remove "123S" and everything before it, I know I can do this with

    string.replaceAll("^.*?(?=[123S])","");
    string.replaceAll("123S","");

But really want to do it in a single expression (can't find another example anywhere!)

like image 881
DaveB Avatar asked May 04 '26 06:05

DaveB


1 Answers

You can do it with:

string.replaceAll("^.*123S","");

Remove non-greedy ? to match last occurence and .* everything before.

like image 80
hsz Avatar answered May 06 '26 20:05

hsz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!