简体   繁体   中英

Specific Regex/Regex Condition

I'm having trouble grabbing a specific piece of text.

My input is:

PMT(B1+B144+B145*1/12.0,B148+B149*1*12.0,B1)

I want to grab all the B1 's, but when I'm trying to do that I'm getting B1 , B144 , B148 , B1 . My first solution was to check the following character. So I came up with the regex B1[\\W] . There are two problems with this: One it ends up grabbing the non word character, and two it doesn't work with "=B1".

How can I grab specific B1 s? For this example I want the first and last B1 .

Edit: I'm using the Java String function replaceAll

确保使用单词边界:

String repl = str.replaceAll("\\bB1\\b", "");

Use B1(?!\\\\d) which means:

  • B1 : match B1
  • (?!\\\\d) : not followed by a digit

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