简体   繁体   中英

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"

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
and my internal check simply as : linktype=\\'internal\\' (for example)

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:

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

It works and it's simple.

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

Either the empty string, or a string with the text 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['"].*$ .

what about something like

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

--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')

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.

Also, to use an "and" condition in a RE you simply concatenate the rules together. This is how the above RE is formed actually. It is (anchor start AND anchor end) OR (an l AND i AND n ... AND character set ['"] AND i AND ... etc...)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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