简体   繁体   English

特定的正则表达式/正则表达式条件

[英]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 . 我想抓住所有的B1 ,但是当我尝试这样做时,我得到的是B1B144B148B1 My first solution was to check the following character. 我的第一个解决方案是检查以下字符。 So I came up with the regex B1[\\W] . 所以我想出了正则表达式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". 这样做有两个问题:一个问题最终是抓住了非单词字符,第二个问题不适用于“ = B1”。

How can I grab specific B1 s? 如何获取特定的B1 For this example I want the first and last B1 . 对于此示例,我想要第一个和最后一个B1

Edit: I'm using the Java String function replaceAll 编辑:我正在使用Java字符串函数replaceAll

确保使用单词边界:

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

Use B1(?!\\\\d) which means: 使用B1(?!\\\\d)表示:

  • B1 : match B1 B1 :匹配B1
  • (?!\\\\d) : not followed by a digit (?!\\\\d) :不后跟数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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