Martin Preusse
Martin Preusse

Reputation: 9369

Match "_<digit>string" wth a regular expression

I have a list of strings like

So I have a string followed by an underscore and "pathway". There may be a digit between the underscore and "pathway". How can I match and replace everything except xxx with a regular expression in Java?

This does not work:

pathnameRaw = pathnameRaw.replace("_\\dpathway","");

Upvotes: 2

Views: 472

Answers (2)

codaddict
codaddict

Reputation: 455122

Your regex is almost fine. Since the digit is optional, add a ? at the end of \\d.

Also the replace method does not use regex. Use replaceAll instead.

See it

Upvotes: 3

khachik
khachik

Reputation: 28693

"_[0-9]?pathway"

Upvotes: 3

Related Questions