Reputation: 133
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 ?
Upvotes: 3
Views: 1869
Reputation: 620
string regex = "^(?:\\n|.*linktype=([\'\"])internal\\1.*\\n)";
var options = RegexOptions.Multiline);
var reg = new Regex(regex, options);
Upvotes: 0
Reputation: 8944
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...)
Upvotes: 0
Reputation: 3402
"<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')
Upvotes: 0
Reputation: 23472
what about something like
(^$)|(^.*linktype=\"internal\".*$)
--Tomas
Upvotes: 0
Reputation: 12469
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['"].*$
.
Upvotes: 1
Reputation: 48596
You could try (^$)|(^.*linktype=\"internal\".*$)
Either the empty string, or a string with the text linktype="internal"
.
Upvotes: 1
Reputation: 24078
Since you mentioned C#, you might as well try this:
if(str.Length == 0 || str.Contains("internal"))
It works and it's simple.
Upvotes: 2