开发者

Regular expression with "|"

开发者 https://www.devze.com 2022-12-24 07:11 出处:网络
I need to be able to check for a pattern with | in them. For 开发者_运维问答example an expression like d*|*t should return true for a string like \"dtest|test\".

I need to be able to check for a pattern with | in them. For 开发者_运维问答example an expression like d*|*t should return true for a string like "dtest|test".

I'm no regular expression hero so I just tried a couple of things, like:

Regex Pattern = new Regex("s*\|*d"); //unable to build because of single backslash
Regex Pattern = new Regex("s*|*d"); //argument exception error
Regex Pattern = new Regex(@"s*\|*d"); //returns true when I use "dtest" as input, so incorrect
Regex Pattern = new Regex(@"s*|*d"); //argument exception error
Regex Pattern = new Regex("s*\\|*d"); //returns true when I use "dtest" as input, so incorrect
Regex Pattern = new Regex("s*" + "\\|" + "*d"); //returns true when I use "dtest" as input, so incorrect
Regex Pattern = new Regex(@"s*\\|*d"); //argument exception error

I'm a bit out of options, what should I then use? I mean this is a pretty basic regular expression I know, but I'm not getting it for some reason.


In regular expressions, the * means "zeros or more (the pattern before it)", e.g. a* means zero or more a, and (xy)* expects matches of the form xyxyxyxy....

To match any characters, you should use .*, i.e.

Regex Pattern = new Regex(@"s.*\|.*d");

(Also, | means "or")

Here . will match any characters[1], including |. To avoid this you need to use a character class:

new Regex(@"s[^|]*\|[^d]*d");

Here [^x] means "any character except x".

You may read http://www.regular-expressions.info/tutorial.html to learn more about RegEx.

[1]: Except a new line \n. But . will match \n if you pass the Singleline option. Well this is more advanced stuff...


A | inside a char class will be treated literally, so you can try the regex:

[|]


How about s.*\|.*d?
The problem of your tries is, that you wrote something like s* - which means: match any number of s(including 0). You need to define the characters following the s by using . like in my example. You can use \w for alphanumerical characters, only.


Try this.

string test1 = "dtest|test";
string test2 = "apple|orange";
string pattern = @"d.*?\|.*?t";

Console.WriteLine(Regex.IsMatch(test1, pattern));
Console.WriteLine(Regex.IsMatch(test2, pattern));


Regex Pattern = new Regex(@"s*\|*d"); would work, except that having |* means "0 or more pipes". So You probably want Regex Pattern = new Regex(@"s.*\|.*d");


In Javascript, if you construct
var regex = /somestuff\otherstuff/;,
then backslashes are as you'd expect. But if you construct the very same thing with the different syntax
var regex = new Regex("somestuff\\otherstuff");
then because of a weirdness in the way Javascript is parsed you have have to double all backslashes. I suspect your first attempt was correct, but you imported a new problem while solving the old in that you ran afoul of this other issue about single backslashes.

0

精彩评论

暂无评论...
验证码 换一张
取 消