子字符串的正则表达式,前面不得有数字

本文关键字:数字 前面 字符串 正则表达式 | 更新日期: 2023-09-27 18:35:36

我用"[a-z][a-z0-9]*"来查找子字符串:

"as4s" - 找到 as4s

"S+SD4" - 找到 S, SD4

"(4ASD SAD)" - 发现 ASD,SAD

"10asd" - 发现 asd

我需要更改此表达式,因此结果将是:

"as4s" - 找到 as4s

"S+SD4" - 找到 S, SD4

"(4ASD 悲伤)" - 发现悲伤

"10asd" - 什么也没找到

您可以使用此代码来测试表达式:

using System.Text.RegularExpressions;
string input = "A*10+5.01E+10";
Regex r = new Regex("[a-zA-Z][a-zA-Z'd]*");
var identifiers = new Dictionary<string, string>();
MatchEvaluator me = delegate(Match m)
{
    Console.WriteLine(m);
    var variableName = m.ToString();
    if (identifiers.ContainsKey(variableName))
    {
        return identifiers[variableName];
    }
    else
    {
        i++;
        var newVariableName = "i" + i.ToString();
        identifiers[variableName] = newVariableName;
        return newVariableName;
    }
};
input = r.Replace(input, me);

子字符串的正则表达式,前面不得有数字

您可以使用单词边界来避免匹配不需要的文本,即前面有一个数字等:

Regex r = new Regex("'b[a-zA-Z][a-zA-Z'd]*'b");

正则表达式演示

(?<!'d)('b[a-z][a-z0-9]*)

试试这个。抓住捕获。请参阅演示。

https://regex101.com/r/gX5qF3/7

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    'd                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  (                        group and capture to '1:
--------------------------------------------------------------------------------
    'b                       the boundary between a word char ('w)
                         and something that is not a word char
--------------------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
--------------------------------------------------------------------------------
    [a-z0-9]*                any character of: 'a' to 'z', '0' to '9'
                         (0 or more times (matching the most
                         amount possible))
--------------------------------------------------------------------------------
  )                        end of '1