简体   繁体   中英

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. So, 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.

------- EDIT -------

I think you have a space character that should not be there. Instead of (REPLICAT | EXTRACT +RUNNING +) use (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

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