简体   繁体   中英

Regular expression : remove words and number

I have a string and I want to remove the string Input! + word + digits Input! + word + digits and Calc! + word + digits Calc! + word + digits from it. I have also included my attempt.

Input : IF(Input!B34 + Calc!B45)
Output : Input!B34 Calc!B45

My attempt :

Pattern findMyPattern = Pattern.compile("Input!\\w\\d|" + worksheetName+ "!.+?");
        Matcher foundAMatch = findMyPattern.matcher(input);
        HashSet hashSet = new HashSet();
        while (foundAMatch.find()) {
            String s = foundAMatch.group(0);
            hashSet.add(s);
        }

What regular expression should I use ? I tried using a few of them. But I am not expert in them. Some idea will be useful.

You can use this regex:

"(?:Input|Calc)![a-zA-Z]\\d+"

Explanation:

(?:Input|Calc)  // Match `Input or Calc`
 !              // Followed by !
[a-zA-Z]        // Followed by an alphabetical character
\\d+            // Then digits.

And use it with Matcher#find and then add matcher.group() to your Set .

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