如何使用正则表达式将数组元素与给定字符串匹配?

huangapple go评论58阅读模式
英文:

How do I match elements of an array to a given string using regex?

问题

public class Needles {
  public static void main(String[] args) {
    String[] needles = new String[]{"abc", "def", "cal", "ghi", "c"};
    findNeedles("cal", needles);
  }

  public static void findNeedles(String haystack, String[] needles) {
    if (needles.length > 5) {
      System.err.println("Too many words!");
    } else {
      int[] countArray = new int[needles.length];
      String[] words = haystack.split("[ '\\t\\n\\b\\f\\r]", 0);
      // String[] words = haystack.split("", 0);
      for (int i = 0; i < needles.length; i++) {
        for (int j = 0; j < words.length; j++) {
          if (words[j].contains(needles[i])) {
            countArray[i]++;
          }
        }
      }
      for (int j = 0; j < needles.length; j++) {
        System.out.println(needles[j] + ": " + countArray[j]);
      }
    }
  }
}
英文:

I have this Java code method which compares the elements of an array of strings with a string variable. This method requires two argument: an argument of type string haystack and an array of strings needles. If the length of the needles array is greater than 5, it outputs an error message to the console. Otherwise it tries to match the elements of haystack with needles using regex. My code returns this:

abc: 0
def: 0
cal: 1
ghi: 0
c: 0

What changes do I need to make so that it matches both cal and c. That is the matching works for multiple character elements as well as well as single character elements?

public class Needles {
  public static void main(String[] args) {
    String[] needles = new String[]{&quot;abc&quot;, &quot;def&quot;, &quot;cal&quot;, &quot;ghi&quot;, &quot;c&quot;};
    findNeedles(&quot;cal&#39;&quot;, needles);

  }

  public static void findNeedles(String haystack, String[]
    needles) {
    if (needles.length &gt; 5) {
      System.err.println(&quot;Too many words!&quot;);
    } else {
      int[] countArray = new int[needles.length];
      String[] words = haystack.split(&quot;[ \&quot;\&#39;\t\n\b\f\r]&quot;, 0);
      //String[] words = haystack.split(&quot;&quot;, 0);
      for (int i = 0; i &lt; needles.length; i++) {
        

        for (int j = 0; j &lt; words.length; j++) {
          if (words[j].compareTo(needles[i]) == 0) {
            countArray[i]++;
          }
        }
      }
      for (int j = 0; j &lt; needles.length; j++) {
        System.out.println(needles[j] + &quot;: &quot; + countArray[j]);
      }
      

    }

  }
}

答案1

得分: 1

你可以直接在 haystack 上使用 contains 方法。在你的第一个 for 循环中,使用类似这样的语句:

if (haystack.contains(needles[i]))
    做一些操作

这里实际上不需要正则表达式。

英文:

You can use the contains method directly on the haystack. In your first for loop use something like:

if(haystack.contains(needles[i])
     doSomething

You dont really need regex here.

huangapple
  • 本文由 发表于 2020年10月5日 08:44:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64201220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定