简体   繁体   English

正则表达式查找带有任何字母的精确计数的单词

[英]Regex to find word with exact count of any letter

I have a list of words: 我有一个词表:

  1. "abac" “算盘”
  2. "abcc" “ abcc”
  3. "acb" “ acb”
  4. "aaaa" “ aaaa”
  5. ... ...

User can input any word for searching in this list. 用户可以在该列表中输入任何单词进行搜索。

My goal is to find in list specific word that contains the same count of each character from input word. 我的目标是在列表中找到包含与输入单词中每个字符相同计数的特定单词。 For example, if an input word is "abca" then only first word from list should match, if "cba" - then only third. 例如,如果输入的词是“ABCA”,那么只能从列表中第一个单词应该匹配,如果“CBA” -那么只有三分之一。

I decided to use regex which will be applied to each words in list separately until matching. 我决定使用正则表达式,它将分别应用于列表中的每个单词,直到匹配为止。

My attempt is regex /^[abca]{4}$/ but this is the wrong approach since it ignores the count of each character therefore second and fourth words from list matching also, though they shouldn't. 我的尝试是正则表达式/^[abca]{4}$/但这是错误的方法,因为它会忽略每个字符的计数,因此列表匹配中的第二个和第四个单词也不会,尽管它们不应该。

Will appreciate any help. 将不胜感激。

A regex doesn't exist for this language because the language is not regular. 该语言不存在正则表达式,因为该语言不是常规语言。

You'll want to either create a HashMap<char, int> that captures the count of each char in the string and compare that to the input string or sort your list of words and the user's input and compare them. 您将需要创建一个HashMap<char, int>来捕获字符串中每个char的计数并将其与输入字符串进行比较,或者对单词列表和用户输入进行排序并进行比较。

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

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