开发者

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

开发者 https://www.devze.com 2023-01-29 15:22 出处:网络
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 \

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


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


"_[0-9]?pathway"

0

精彩评论

暂无评论...
验证码 换一张
取 消