简体   繁体   English

正则表达式:如何制作条件正则表达式

[英]Regular expressions : How to make a conditional regex

I have the following conditions for my regex : When the string is not empty it should contain the word "internal" 我的正则表达式有以下条件:当字符串不为空时,它应包含单词“internal”

So in other words : 换句话说:

"<link linktype='internal' id='{F88AE8AE-69C4-4E31-95BF-73B110FEE63A}' />" --> OK
"<link linktype='external' id='{F88AE8AE-69C4-4E31-95BF-73B110FEE63A}' />" --> NOK
"test" --> NOK
"" --> OK

I know that an empty string can be checked with : ^$ 我知道可以使用以下命令检查空字符串: ^$
Or a non empty string with : ^\\s*\\S 或者是非空字符串,其中包含: ^\\s*\\S
and my internal check simply as : linktype=\\'internal\\' (for example) 我的内部检查只是: linktype=\\'internal\\' (例如)

Bringing them together is the hard part. 将它们结合在一起是困难的部分。 I've been stuck on this, but it doesn't do as expected : 我一直坚持这个,但它没有按预期做到:

(?(?=^\s*\S)linktype=\"internal\"|^$)

Can anyone help ? 有人可以帮忙吗?

Since you mentioned C#, you might as well try this: 既然你提到了C#,你不妨试试这个:

if(str.Length == 0 || str.Contains("internal"))

It works and it's simple. 它有效,而且很简单。

You could try (^$)|(^.*linktype=\\"internal\\".*$) 你可以尝试(^$)|(^.*linktype=\\"internal\\".*$)

Either the empty string, or a string with the text linktype="internal" . 空字符串或文本linktype="internal"的字符串。

In this particular case, you can use: 在这种特殊情况下,您可以使用:

^(.*linktype=['"]internal['"].*)?$

Otherwise, it is easier to write a regex for each case separately and then enclose them in parenthesis and use a 'or' to include them in a single expression: 否则,分别为每个案例编写一个正则表达式然后将它们括在括号中并使用'或'将它们包含在单个表达式中更容易:

(^$)|(^.*linktype=['"]internal['"].*$)

This will match either ^$ or ^.*linktype=['"]internal['"].*$ . 这将匹配^$^.*linktype=['"]internal['"].*$

what about something like 怎么样的

(^$)|(^.*linktype=\"internal\".*$)

--Tomas --Tomas

这样的事情应该这样做:

^.*linktype='(internal|)'.*$
"<link linktype='internal' id='{F88AE8AE-69C4-4E31-95BF-73B110FEE63A}' />" --> OK
"<link linktype='external' id='{F88AE8AE-69C4-4E31-95BF-73B110FEE63A}' />" --> NOK
"test" --> NOK
"" -->

If linktype='internal' is matched you don't really care of what is before and after the linktype='internal', you will anyway get a match: 如果linktype ='internal'匹配你并不关心linktype ='internal'之前和之后的内容,那么无论如何你都会得到一个匹配:

(^$)|(linktype='internal')

Perhaps I should add my own answer. 也许我应该添加自己的答案。 The answers so far have used capturing groups which are slightly more costly. 到目前为止,答案使用的捕获组略高一些。 To use an "or" condition with a non-capturing group: 要对非捕获组使用“或”条件:

(?:^$)|(?:linktype=['\"]internal['\"])

There is no need for anchors on the second part as an RE by definition will match anywhere within the string without the anchors. 在第二部分上不需要锚点,因为根据定义,RE将匹配字符串中的任何地方而没有锚点。

Also, to use an "and" condition in a RE you simply concatenate the rules together. 此外,要在RE中使用“和”条件,您只需将规则连接在一起即可。 This is how the above RE is formed actually. 这就是上面的RE实际形成的方式。 It is (anchor start AND anchor end) OR (an l AND i AND n ... AND character set ['"] AND i AND ... etc...) 它是(锚开始和结束锚)OR(一li AND n ... AND字符集['“] AND i AND ...等...)

string regex = "^(?:\\n|.*linktype=([\'\"])internal\\1.*\\n)";
var options = RegexOptions.Multiline);
var reg = new Regex(regex, options);

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

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