Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式以匹配类似剃刀的表达式_Regex_Regex Negation - Fatal编程技术网

Regex 正则表达式以匹配类似剃刀的表达式

Regex 正则表达式以匹配类似剃刀的表达式,regex,regex-negation,Regex,Regex Negation,我一直在试图找出如何匹配剃刀般的嵌入式表达式。这不是真正的Razor语法,只是一些类似的东西 示例: @ # Match @ (\()? # Optionally match `(` and capture in group 1 ( # Match and capture in group 2 [A-Za-z]+ # 1+ ASCII letters [0-9]* # 0+ ASCII digits ) # E

我一直在试图找出如何匹配剃刀般的嵌入式表达式。这不是真正的Razor语法,只是一些类似的东西

示例:

@           # Match @
(\()?       # Optionally match `(` and capture in group 1
(           # Match and capture in group 2
 [A-Za-z]+  # 1+ ASCII letters
 [0-9]*     # 0+ ASCII digits
)           # End of capturing group
(?(1)       # If group 1 participated in the match
 \)         # match a closing parenthesis
)           # End of conditional
给定以下字符串:

这个@ShouldMatch1和这个@ShouldMatch2和这个 @((不应匹配1)andthis@ShouldMatch3)而这个(不应该匹配)2 而这个@1不应该匹配3和这个((不应该匹配4和这个) @(应该匹配4)

  • 匹配和捕获:
    • 应该匹配1
    • 应该匹配2
    • 应该配对吗
    • 应该配对吗
基本上,以下是要求:

  • 如果它以@开头,然后是[a-zA-Z]+[0-9]*那么我想匹配它
  • 如果它以@开头(,那么我只想匹配后面跟[a-zA-Z]+[0-9]*然后是a)
这是我的一个开始,它在大多数情况下都有效,但它匹配不应匹配2

\@[(]?([a-zA-Z]+[0-9]*[)]*)
它要么是
@(foo1)
要么是
@foo1
,因此请使用替代选项:

@([a-zA-Z]+[0-9]|\([a-zA-Z]+[0-9]\))
并在第二步(简单字符串替换)中去掉括号。

此代码:

!/usr/bin/env perl

$\ux=如果您的正则表达式引擎支持条件:

@(\()?([A-Za-z]+[0-9]*)(?(1)\))
说明:

@           # Match @
(\()?       # Optionally match `(` and capture in group 1
(           # Match and capture in group 2
 [A-Za-z]+  # 1+ ASCII letters
 [0-9]*     # 0+ ASCII digits
)           # End of capturing group
(?(1)       # If group 1 participated in the match
 \)         # match a closing parenthesis
)           # End of conditional

这些标识符是否真的应该是一个或多个字母后跟一个或多个数字
\pL+\d+
,还是应该是0个或多个数字
\pL+\d*
?所有字母是否总是在数字之前?还是应该仅以字母开头,然后将0个或多个字母和数字的任意组合混合在一起
\pL[\d]*
?你真的想排除下划线,还是想
\pL\w*
(?=\pL)\w+)(?谢谢大家。我在我新的.NET字符串扩展库中使用了这个名为“Expansive”:嗨,Andery,只是一个注释-是受你的正则表达式的启发,用razor语法制作了这个JSFIDLE下划线模板:)(它对条件句tho不起作用,而且它确实会阻塞some@email.com)您不需要使用第二步。必须稍微修改以使0-9为零或更多:@([a-zA-Z]+[0-9]*|([a-zA-Z]+[0-9]*))完美!我正在测试条件匹配和分组,但无法使其工作。谢谢!+1我非常确定您不需要条件中的else分支。如果您有
(?(条件)YES-PART | NO-PART)
并且没有空的NO-PART,您应该可以只编写
(?(条件)YES-PART)
。至少,在Perl.BTW下运行您的模式时,我不需要它。顺便说一句,匹配/捕获的内容中确实包含At符号和paren,我试图避免这些,因为最初的问题似乎不希望基于他预期的匹配集的“匹配和捕获”列表。@tchrist:啊,是的,没错。谢谢你的提示。@Andery实际上想要捕获的东西当然是
$2
。@Tim哦,对了,duh。我只是抓住了整场比赛,不只是$2。做得好。我会用这个,但我需要在.NET上工作的东西。谢谢。@Andery:你真的应该把.NET放在这个任务中ion tagset.:(不过,这不重要,因为即使是邪恶帝国也支持这样的命名捕获。你只需要自己拉
id
组。另外,确保前缀为
(?x)
或一些编译标志,如Java的
模式。注释
以启动
/x
模式,这样你就可以使用空格来查看你在做什么。但是,我不能确切地告诉你如何操作,因为我只是一个不使用windows的正则表达式少女。你可能还需要笨拙的大括号:
\p{L}
。谢谢你提供的额外信息。我的缺点是没有把.NET放在标签集中。我提到了Razor(MVC的.NET视图引擎),但我忘了明确提到.NET。
@           # Match @
(\()?       # Optionally match `(` and capture in group 1
(           # Match and capture in group 2
 [A-Za-z]+  # 1+ ASCII letters
 [0-9]*     # 0+ ASCII digits
)           # End of capturing group
(?(1)       # If group 1 participated in the match
 \)         # match a closing parenthesis
)           # End of conditional