简体   繁体   English

正则表达式:删除单词和数字

[英]Regular expression : remove words and number

I have a string and I want to remove the string Input! + word + digits 我有一个字符串,我想删除字符串Input! + word + digits Input! + word + digits and Calc! + word + digits Input! + word + digitsCalc! + word + digits Calc! + word + digits from it. Calc! + word + digits 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 . 并将其与Matcher#find ,然后将matcher.group()添加到Set

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

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