简体   繁体   中英

Match “_<digit>string” wth a regular expression

I have a list of strings like

  • xxx_2pathway
  • xxx_6pathway
  • xxx_pathway

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","");

"_[0-9]?pathway"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM