【问题标题】:Regex to find all placeholder occurrences in text正则表达式查找文本中出现的所有占位符
【发布时间】:2021-08-20 16:56:03
【问题描述】:

我正在努力创建一个正则表达式来查找给定文本中出现的所有占位符。占位符的格式如下:

[{PRE.Word1.Word2}]

规则:

  • 由“[{PRE.”分隔。和“}]”(“PRE”大写)
  • 2 个单词(每个单词至少 1 个字符长),用点分隔。除换行符外,每个单词上的所有字符均有效。
  • word1:最少 1 个字符,最多 15 个字符
  • word2:最少 1 个字符,最多 64 个字符
  • word1 不能有点,如果占位符内的点超过 2 个,多余的点将成为 word2 的一部分。如果少于 2 个点,占位符无效。

无论这两个词是什么,都希望获得所有有效的占位符。

我不是懒惰,只是花了很多时间在 regexr.com 上建立规则,但无法跨越所有这些规则。 期待检查您的建议。

我最接近的是下面的内容,任何扩展它的尝试都会破坏所有有效的匹配。

\[\{OEP\.*\.*\}\]

非常感谢!

Regex 应在其中找到匹配项的示例文本:

此处为随机文字

[{Test}] -- 不匹配

[{PRE.TestTest3}] --不匹配

[{PRE.TooLong.12345678901234567890}] --不匹配

[{PRE.Address.Country}] --匹配

[{PRE.Version.1.0}] --匹配

此处为随机文字

【问题讨论】:

  • 这些词可以包含{}[] 字符吗?好吧,试试\[{PRE\.([^][{}.]{1,15})\.(.{1,64}?)}],见this regex demo。我不确定您期望什么输出,如果您提供一两个测试用例,我可以发布带有代码和解释的答案。
  • @WiktorStribiżew 看准了!该正则表达式似乎可以解决问题,如果您作为答案提交,我会接受。
  • 杰米,我把它贴在below

标签: c# regex


【解决方案1】:

你可以使用

\[{PRE\.([^][{}.]{1,15})\.(.{1,64}?)}]

regex demo

详情

  • \[{ - [{ 字符串
  • PRE\. - PRE. 文字
  • ([^][{}.]{1,15}) - 第 1 组:除 []{}. 之外的任何 1 到 15 个字符
  • \. - 一个点
  • (.{1,64}?) - 任何 1 到 64 个字符,除了换行符,尽可能少
  • }] - }] 文本。

如果需要在 C# 中获取所有匹配项,可以使用

var pattern = @"\[{PRE\.([^][{}.]{1,15})\.(.{1,64}?)}]";
var matches = Regex.Matches(text, pattern);

this C# demo:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {
        var text = "[{PRE.Word1.Word2}] and [{PRE.Word 3.Word..... 2 %%%}]";
        var pattern = @"\[{PRE\.([^][{}.]{1,15})\.(.{1,64}?)}]";
        var matches = Regex.Matches(text, pattern);
        var props = new List<Property>();
        foreach (Match m in matches)
            props.Add(new Property(m.Groups[1].Value,m.Groups[2].Value));
        
        foreach (var item in props)
            Console.WriteLine("Word1 = " + item.Word1 + ", Word2 = " + item.Word2);
    }
    
    public class Property
    {
        public string Word1 { get; set; }
        public string Word2 { get; set; }
        public Property()
        {}
        public Property(string w1, string w2)
        {
            this.Word1 = w1;
            this.Word2 = w2;
        }
    }
}

输出:

Word1 = Word1, Word2 = Word2
Word1 = Word 3, Word2 = Word..... 2 %%%

【讨论】:

    【解决方案2】:
    string input = "[{PRE.Word1.Word2}]";
    
    // language=regex
    string pattern = @"\[{ PRE \. (?'group1' .{1,15}? ) \. (?'group2' .{1,64}? ) }]";
    
    var match = Regex.Match(input, pattern, RegexOptions.IgnorePatternWhitespace);
    
    Console.WriteLine(match.Groups["group1"].Value);
    Console.WriteLine(match.Groups["group2"].Value);
    

    【讨论】:

    • 谢谢。但是当得到所有占位符时,我还不知道这些词是什么。只是想获得所有匹配项(无论单词是什么)。
    • @JaimeOliveira 使用Regex.Matches 而不是Regex.Match
    • @JaimeOliveira - 我更改了组名以使其更清晰。
    • @Alexander 你也应该让.{1,64} 变得懒惰。否则[{PRE.Word1.Word2}] [{PRE.Word3.Word4}] 将被视为一场比赛。
    • 你的图案中嵌入的所有空格有什么意义?我可以让你的模式工作,但只有在删除空格之后。即,这对我有用:@"\[{PRE\.(?'group1'.{1,15}?)\.(?'group2'.{1,64}?)}]"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 2023-03-06
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    相关资源
    最近更新 更多