正则表达式:我想这和那...以任意顺序 [英] Regex: I want this AND that AND that... in any order

查看:122
本文介绍了正则表达式:我想这和那...以任意顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我甚至不知道这是可能的或没有,但这里想什么我。

I'm not even sure if this is possible or not, but here's what I'd like.

String: "NS306 FEBRUARY 20078/9/201013B1-9-1Low31 AUGUST 19870"

我有一个文本框,我在搜索参数类型,他们是空格分隔。正因为如此,我想回到一个匹配字符串1是字符串中,然后string2为字符串中,或​​string2为字符串中,然后字符串1是的字符串中。我不在乎什么命令字符串的,但他们都(会出头的我比2)必须是在字符串中

I have a text box where I type in the search parameters and they are space delimited. Because of this, I want to return a match is string1 is in the string and then string2 is in the string, OR string2 is in the string and then string1 is in the string. I don't care what order the strings are in, but they ALL (will somethings me more than 2) have to be in the string.

因此​​,例如,提供的字符串在我希望

So for instance, in the provided string I would want:

"FEB Low"

"Low FEB"

...返回作为搭配。

...to return as a match.

我是真正的新的正则表达式,只读在一些教程这里但那是前一阵子,我需要到今天完成这个工作。周一我开始一个新的项目,这是更为重要,不能用这个问题而分心。反正是有定期的前pressions要做到这一点,还是我不得不通过搜索过滤器的每一个部分进行迭代和排列替换顺序?任何及所有帮助是非常AP preciated。谢谢你。

I'm REALLY new to regex, only read some tutorials on here but that was a while ago and I need to get this done today. Monday I start a new project which is much more important and can't be distracted with this issue. Is there anyway to do this with regular expressions, or do I have to iterate through each part of the search filter and permutate the order? Any and all help is extremely appreciated. Thanks.

更新:
我不想之所以要通过迭代循环,并正在寻找最佳的性能明智的,因为不幸的是,我使用的电话上的每个键preSS这个功能,数据表,我不希望它陷入困境下来。

UPDATE: The reason I don't want to iterate through a loop and am looking for the best performance wise is because unfortunately, the dataTable I'm using calls this function on every key press, and I don't want it to bog down.

更新:
谢谢大家的帮助,这是多少AP preciated。

UPDATE: Thank you everyone for your help, it was much appreciated.

code更新:

归根结底,这是我跟去了。

Ultimately, this is what I went with.

string sSearch = nvc["sSearch"].ToString().Replace(" ", ")(?=.*");
if (sSearch != null && sSearch != "")
{
  Regex r = new Regex("^(?=.*" + sSearch + ").*$", RegexOptions.IgnoreCase);
  _AdminList = _AdminList.Where<IPB>(
                                       delegate(IPB ipb)
                                       {
                                          //Concatenated all elements of IPB into a string
                                          bool returnValue = r.IsMatch(strTest); //strTest is the concatenated string
                                          return returnValue;
                                    }).ToList<IPB>();
                                       }
}

的平局类有元素X个和在整个我正在网站上没有一个表都以相同的顺序列。因此,我需要的任何命令搜索和我不希望有写很多code的去做。有在这里等好主意,但我知道我的老板很喜欢正则表达式(preaches他们),因此我认为这会是最好的,如果我和那去了现在。如果出于某种原因,网站的性能滑倒(Intranet网站),那么我会尝试另一种方式。谢谢大家。

The IPB class has X number of elements and in no one table throughout the site I'm working on are the columns in the same order. Therefore, I needed to any order search and I didn't want to have to write a lot of code to do it. There were other good ideas in here, but I know my boss really likes Regex (preaches them) and therefore I thought it'd be best if I went with that for now. If for whatever reason the site's performance slips (intranet site) then I'll try another way. Thanks everyone.

推荐答案

您可以使用(= ...?) 正向前查找;它声称,一个给定的图案,可以匹配。你会停泊在字符串的开头,一个接一个,以任意顺序,查找匹配您的每一个模式。

You can use (?=…) positive lookahead; it asserts that a given pattern can be matched. You'd anchor at the beginning of the string, and one by one, in any order, look for a match of each of your patterns.

这将是这个样子:

^(?=.*one)(?=.*two)(?=.*three).*$

这将匹配包含字符串两节 三化,以任意顺序(如看到rubular.com )。

This will match a string that contains "one", "two", "three", in any order (as seen on rubular.com).

根据上下文,您可能需要 href=\"http://www.regular-ex$p$pssions.info/anchors.html\">锚 \\ A 和 \\ Z ,并使用单行模式等等的the点匹配的一切。

Depending on the context, you may want to anchor on \A and \Z, and use single-line mode so the dot matches everything.

这还不是最有效的解决问题的办法。最好的解决办法是解析出在你的输入,并把它变成一个高效的集重新presentation等字样。

This is not the most efficient solution to the problem. The best solution would be to parse out the words in your input and putting it into an efficient set representation, etc.

  • How does the regular expression (?<=#)[^#]+(?=#) work?

让我们说,我们希望我们的密码:

Let's say that we want our password to:


  • 包含8个至15个字符

  • 必须包含一个大写字母

  • 必须包含一个小写字母

  • 必须包含一个数字

  • 必须包含特殊符号之一

然后,我们可以写一个正则表达式是这样的:

Then we can write a regex like this:

^(?=.{8,15}$)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*]).*$
 \__________/\_________/\_________/\_________/\______________/
    length      upper      lower      digit        symbol

这篇关于正则表达式:我想这和那...以任意顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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