简体   繁体   English

正则表达式问题

[英]Regular Expressions Question

I have this program:我有这个程序:

        Dim words() As String = {"car", "arc", "caar"}

        For Each w In words
            Dim rx = Regex.IsMatch("rca", "^[" + w + "]+$")
            Console.WriteLine(rx)
        Next

        Console.ReadLine()

This Regex "^[" + w + "]+$" finds all words which consists of letters "rca".此正则表达式"^[" + w + "]+$"查找由字母 "rca" 组成的所有单词。 This matches for all words, because all words are made up from "rca".这匹配所有单词,因为所有单词都是由“rca”组成的。 Is there something I could add, to return False for "caar", because "rca" has only one "a", but "caar" has two "a"?有什么我可以添加的,为“caar”返回 False,因为“rca”只有一个“a”,但“caar”有两个“a”?

This Regex "^[" + w + "]+$" finds all words which consists of letters "rca"此正则表达式"^[" + w + "]+$"查找由字母"rca"组成的所有单词

No, it does not.不,不是的。 It searches the string "rca" for all the letters that happen to be in w它在字符串"rca"中搜索所有碰巧在w中的字母

What you mean (given that your plain English explanation reflects what you want) is:您的意思是(鉴于您的简单英语解释反映了您想要的)是:

Dim rx = Regex.IsMatch(w, "^[rca]+$")

*You could change + to {3} , but this would still match aaa . *您可以将+更改为{3} ,但这仍然匹配aaa

To match any permutation of three letters, you would have to add permutations yourself.要匹配三个字母的任何排列,您必须自己添加排列。 Regex can't do this for you.正则表达式无法为您做到这一点。

Dim rx = Regex.IsMatch(w, "^rca|rac|acr|arc|car|cra$")

You can do it eg with the following regular expression:您可以使用以下正则表达式来执行此操作:

"(?=^[^r]*r[^r]*$)(?=^[^c]*c[^c]*$)(?=^[^a]*a[^a]*$)^[rca]+$"

It matches any word consisting of letters "rca" but each one at exactly once.它匹配任何由字母“rca”组成的单词,但每个单词都恰好匹配一次。

Addon : if the condition is "at most once" you can instead use插件:如果条件是“最多一次”,您可以改为使用

"(?=^[^r]*r?[^r]*$)(?=^[^c]*c?[^c]*$)(?=^[^a]*a?[^a]*$)^[rca]+$"

You would have to do it separately, outside the regex.您必须在正则表达式之外单独执行此操作。 The [...] construct always treats repeated characters as if they were entered once. [...]构造总是将重复的字符视为输入一次。 You could do something like this right before the Console.WriteLine(rx) (note: writing in C# because I'm not very current in VB):你可以在Console.WriteLine(rx)之前做这样的事情(注意:写在 C# 因为我在 VB 中不是很新):

foreach (var ch in w.ToCharArray())
{
    if (w.Count(c => c == ch) != "rca".Count(c => c == ch))
        return false;
}

Currently, it looks like you're using the words as the pattern to search for, perhaps you mean:目前,您似乎正在使用单词作为搜索模式,也许您的意思是:

Dim rx = Regex.IsMatch(w, "^[rca]+$")

In order to filter out words that contain only one of the letters in your pattern ( rca ), you might try:为了过滤掉仅包含模式中一个字母的单词( rca ),您可以尝试:

^[^rca]*[rca][^rca]*$

This will match这将匹配

"anything not r, c or a" zero or many times; 
"r, c or a"; 
"anything not r, c or a" zero or many times;

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

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