【问题标题】:Java, can regex's take variables?Java,正则表达式可以接受变量吗?
【发布时间】:2017-06-29 13:30:41
【问题描述】:

我将 REGEX 视为解决问题的可能方法。我制作了一些示例字符串,其中包含诸如 hello、hllllooo、hhhheello 等单词。然后我创建了一个正则表达式来查找所有这些类型的单词。我不应该做的是查看可能包含或不包含输入单词的句子。例如,您输入helloloo,我想扫描我的句子以查找类似于“helloloo”的单词,如果在句子中找到则返回“hello”。我可以创建一个作为用户输入变量的正则表达式吗?如果你输入 hellloo,那么我会构造一些东西,从句子或文件中返回相似的词。

两个输入字符串

String line = "this is hello helloooo hellllooo hhhel hellll what can I do?";
String longLine = "hello man this what can up down where hey my there find now ok stuff jive super sam dude car";

我的正则表达式函数

public static void regexChecker(String theRegex, String str2Check) {
        Pattern checkRegex = Pattern.compile(theRegex);
        Matcher regexMatcher = checkRegex.matcher(str2Check);

        while(regexMatcher.find()) {
            if(regexMatcher.group().length() != 0) {
                System.out.println(regexMatcher.group().trim());
            }
        }
    }

运行这个

regexChecker("\\s[h]*[e]*[l]*[l]*[o]*\\s", line);

返回

hello
hello
hellllooo
hellll

我想根据用户输入“helllooo”创建一个正则表达式,它从第二个字符串 longLine 返回 hello。不确定正则表达式是否是正确的解决方案,但我想知道它是否可能。

【问题讨论】:

  • 你为什么把这些字母放在[]?别。 --- 你真的希望这些字母是可选的,所以它匹配例如ho这个词?将 * 更改为 + 以要求每个字母中至少有一个。 --- 将\\s 都替换为\\b 以匹配单词边界。 --- 正则表达式应该是"\\bh+e+l+l+o+\\b"。请参阅regex101 进行演示。
  • 谢谢,应该是+

标签: java regex string match


【解决方案1】:

假设您的示例捕获了您想要做的事情(输入单词中的重复字母),您绝对可以。

从概念上讲,您需要以下内容:

public String makeRegec(String input) {
    StringBuilder regex = new StringBuilder("\\b");
    if(input != null && input.length() > 0) {

        for (int i = 0; i < input.length(); i++) {
            regex.append(input.charAt(i)).append("+");
        }
    }
    regex.append("\\b*");//don't need this if you would accept hello, for example
    return regex.toString();
}

当然,你可以编译模式并返回它

【讨论】:

    【解决方案2】:

    试试这个:

    String found = input.replaceAll(".*?((h)+(e)+(l)(l)+(o)+)?.*", "$2$3$4$5$6");
    

    如果输入中没有类似 hello 的内容,则结果将是“hello”或空白。

    【讨论】:

      【解决方案3】:

      试试这个

      制作正则表达式函数:

      public static String makeRegex(String input) {
      StringBuilder regex = new StringBuilder();
      if(input != null && input.length() > 0) {
      
          for (int i = 0; i < input.length(); i++) {
              regex.append("[" + input.charAt(i) + "]{1,}");
      
          }
      }
      return regex.toString();
      }
      

      正则表达式函数:

      public static ArrayList regexChecker(String theRegex, String str2Check) {
              Pattern checkRegex = Pattern.compile(theRegex);
              Matcher regexMatcher = checkRegex.matcher(str2Check);
             ArrayList<String> list = new ArrayList<>();
             String findString = "";
              while(regexMatcher.find()) {
                  if(regexMatcher.group().length() != 0) {
                      findString = regexMatcher.group().trim();
                      list.add(findString);
      
                  }
              }
              return list;
          }
      

      使用:

      String line = "this is hello helloooo hellllooo hhhel hellll what can I do?";
      
          String input = "hello";
      
          ArrayList<String> list = regexChecker(makeRegex(input), line);
      
          for(int i = 0;i<list.size();i++)
              System.out.println(list.get(i));
      

      返回:

      hello
      helloooo
      hellllooo
      

      替换字符串:

      System.out.println(line.replaceAll(makeRegex(input), input));
      

      【讨论】:

      • 我已经可以得到那个输出了。我正在尝试输入 Heeeelllooo 并返回 Hello
      • 使用 ReplaceAll: System.out.println(line.replaceAll(makeRegex(input), input));
      • 为什么在您的正则表达式生成器中使用 {1,} 而不是 +?另外,如果有两个“l”,那么像 Hello 这样的词呢?正则表达式中的第一个 l 会吃掉所有的 ls 吗?
      猜你喜欢
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2012-09-14
      • 2012-08-28
      • 1970-01-01
      相关资源
      最近更新 更多