如何为此编写正则表达式 [英] How to write regex expression for this

查看:82
本文介绍了如何为此编写正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



请告诉我如何在RegX中写下以下内容



字符串a = 这是一个测试字符串

string b =在应用程序中将其用作测试字符串



if(a || b )//即a或b。怎么用表达形式写这个?

{

.....

.......

.........

}

谢谢

Hi all

Please tell me how to write the below in RegX

String a = "this is a test string"
string b = "Use this as a test string in application"

if ( a || b ) //i.e. a or b . how to write this in expression form
{
.....
.......
.........
}
Thank you

推荐答案

如果你想要要在代码中使用正则表达式,最好先了解它是如何工作的,否则如果你以后需要更改表达式,你会遇到麻烦。

这是我经常去的一个网站当我需要了解更多。 正则表达式教程 [ ^ ]



拥有一个可以测试不同表达式的正则表达式工具也很不错关于各种输入数据。这是我经常使用的免费工具: http://sourceforge.net/projects/regextest/ [ ^ ]



解决你的问题问题我会创建一个可以匹配两个字符串的正则表达式

If you want to use regular expressions in your code it is a good idea to learn how it works, because otherwise you will run into trouble if you need change the expression later on.
Here is a web site I usually go to when I need to learn more. Regular Expressions Tutorial[^]

It is also good to have a regex tool where you can test different expressions on a variety of input data. Here is a free tool I usually use: http://sourceforge.net/projects/regextest/[^]

To solve your problem I would create a regular expression that can match both strings
\ba test string\b



边界\ b用于确保单词以正确的顺序出现。



c#代码如下所示:


The word boundaries \b is used to make sure the words appear in the correct sequence.

The c# code will look something like this:

Regex regex = new Regex(@"\ba test string\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
if (regex.IsMatch(a) || regex.IsMatch(b))
{
    // do some work;
}





这个例子当然受到你在问题中提供的数据的限制。



This example is of course limited by the data you have given in your question.


这篇关于如何为此编写正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆