簡體   English   中英

正則表達式匹配由 1 個空格或 1 個連字符分隔的單詞

[英]Regular Expression matching words separated by 1 space or 1 hyphen

我需要匹配具有以下模式的字符串:“ExactWord (Pattern)”

模式由一個或多個單詞組成,每個單詞由一個空格或一個連字符分隔。 如果在左括號后立即找到空格或連字符,則不應匹配。 如果在右括號之前發現空格或連字符,則不應匹配。

我目前的問題是我無法編寫不匹配多個空格和多個連字符的模式:

using System;
using System.Text.RegularExpressions;

namespace Code
{
    public static class Program
    {
        public static void Main()
        {
            string[] str = new string[] { "CorrectWord (black-tailed jackrabbit)",
                "IncorrectWord (saber-tooth)",
                "CorrectWord(animal)", 
                "CorrectWord bird", 
                "CorrectWord (Lion)", 
                "CorrectWordNot (Eagle)", 
                "CorrectWord (Harpy eagle)", 
                "CorrectWord (One Two Three)", 
                "CorrectWord (Too  Many  Spaces)", //should not match, but does.
                "CorrectWord (One-two-three)", 
                "CorrectWord (Too--Many Hypens)", //should not match, but does
                "CorrectWord ( leading-white-space)", 
                "CorrectWord (trailing white space )" 
            };
            foreach (string s in str)
            {
                if (Regex.IsMatch(s, @"^CorrectWord" + Regex.Escape(" (") + @"\w+[\w+(\s|\-)?]+\w+" + Regex.Escape(")") + "$"))
                {
                    Console.WriteLine(s);
                }
            }
        }
    }
}

輸出:

CorrectWord (black-tailed jackrabbit)
CorrectWord (Lion)
CorrectWord (Harpy eagle)
CorrectWord (One Two Three)
CorrectWord (Too  Many  Spaces)
CorrectWord (One-two-three)
CorrectWord (Too--Many Hypens)

你可以試試

 \ACorrectWord \([A-Za-z]+(?:[ -][A-Za-z]+)*\)\z

圖案; 這里

 \A            - anchor, string start
  CorrectWord  - correct word as it should be
               - ' ' (space)
 \(            - '('
 [A-Za-z]+     - word (one or more chars in A..Z or a..z range)
 (?: ... )*    - followed by zero or more chunks which are
 [ -][A-Za-z]+ - hyphen or space [ -] then word [A-Za-z]+
 \)            - ')'
 \z            - anchor, end of the string 

演示:

  string[] str = new string[] { 
    "CorrectWord (black-tailed jackrabbit)",
    "IncorrectWord (saber-tooth)",
    "CorrectWord(animal)",
    "CorrectWord bird",
    "CorrectWord (Lion)",
    "CorrectWordNot (Eagle)",
    "CorrectWord (Harpy eagle)",
    "CorrectWord (One Two Three)",
    "CorrectWord (Too  Many  Spaces)", //should not match, but does.
    "CorrectWord (One-two-three)",
    "CorrectWord (Too--Many Hypens)", //should not match, but does
    "CorrectWord ( leading-white-space)",
    "CorrectWord (trailing white space )"
  };

  Regex regex = new Regex(@"\ACorrectWord \([A-Za-z]+(?:[ -][A-Za-z]+)*\)\z");

  var result = str
    .Select(test => $"{test,-40} :: {(regex.IsMatch(test) ? "match" : "failed")}");

  Console.Write(string.Join(Environment.NewLine, result));

結果:

CorrectWord (black-tailed jackrabbit)    :: match
IncorrectWord (saber-tooth)              :: failed
CorrectWord(animal)                      :: failed
CorrectWord bird                         :: failed
CorrectWord (Lion)                       :: match
CorrectWordNot (Eagle)                   :: failed
CorrectWord (Harpy eagle)                :: match
CorrectWord (One Two Three)              :: match
CorrectWord (Too  Many  Spaces)          :: failed
CorrectWord (One-two-three)              :: match
CorrectWord (Too--Many Hypens)           :: failed
CorrectWord ( leading-white-space)       :: failed
CorrectWord (trailing white space )      :: failed

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM