简体   繁体   English

Java Groovy提取两个字符串

[英]java groovy to extract two strings

I have the following regular expression to find the word called EXTRACT, but I need to find another work called REPLICAT. 我有以下正则表达式来查找名为EXTRACT的单词,但我需要查找另一项名为REPLICAT的工作。 So, REPLICAT | 因此,REPLICAT | EXTRACT. 提取。 How to do this in one expression. 如何在一个表达式中做到这一点。

def matcher =  rawTerminalText =~ /(?m)(EXTRACT +RUNNING +)(.*?\w)( +)(\d{2}):(\d{2}):(\d{2})( +)(.*?\w)( +)(.*?$)/

I tried the following, but its not working. 我尝试了以下操作,但无法正常工作。

def matcher =  rawTerminalText =~ /(?m)((^| )(REPLICAT|EXTRACT)+$ +RUNNING +)(.*?\w)( +)(\d{2}):(\d{2}):(\d{2})( +)(.*?\w)( +)(.*?$)/

I suggest using a regex testing site, that has explanations on the regex and allows testing with sample text. 我建议使用正则表达式测试站点,该站点在正则表达式上有解释,并允许使用示例文本进行测试。

I personally like to use this one: https://regex101.com/ as it has very good explanations on the written regex. 我个人喜欢使用这个: https : //regex101.com/,因为它对书面正则表达式有很好的解释。

------- EDIT ------- -------编辑-------

I think you have a space character that should not be there. 我认为您的空格字符不应该存在。 Instead of (REPLICAT | EXTRACT +RUNNING +) use (REPLICAT|EXTRACT +RUNNING +) 代替(REPLICAT | EXTRACT + RUNNING +),使用(REPLICAT | EXTRACT + RUNNING +)

----- END EDIT ----- -----结束编辑-----

If you wanted to know only about those given word which is present in the string or not? 您是否只想知道字符串中是否存在那些给定的单词? then uses below regular expression? 然后使用下面的正则表达式?

def rawText=" The Word which i am looking for is, REPLICAT and EXTRACT"
def Word= /^.*\b(REPLICAT|EXTRACT)\b.*$/;
def findWord= (rawText =~ /$Word/);
log.info  findWord.count;

    Output:
    count will return 1: found
    count will return 0:not found

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

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