正则表达式与单词序列维持 [英] Regular expression with word sequence maintain

查看:77
本文介绍了正则表达式与单词序列维持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为正则表达式编写模式以保持单词序列。



例如:



我有一个如下字符串:



I would like to write pattern for regular expression which will maintain the sequence of word.

For Example:

I have one string like below:

string input="I visited this place with my friends twice. Normal outdoor sitting but yet pleasing. Food is very delicious and sumptuous. However they highly lack in Service which they need to improve a lot. On both my visit they failed to deliver the complete order and on asking twice - thrice, they finally came after half and hour and said that \" sorry we didn't prepare that, shall we prepare it now\". It was pretty disappointing as it happened repeatedly. I would have giving this place a 5 star but due to their poor service I will give them just 2 Stars."

    string strPattern = "(visit(.*?) this place)";
    Match matchResult = Regex.Match(input, strPattern, RegexOptions.IgnoreCase);
     while(matchResult.Success) {
     Console.WriteLine(matchResult.Value);
     matchResult = matchResult.NextMatch();
    }





当我控制它时,它会输出像

<参观这个地方

2.访问他们未能完成整个订单并且两次询问 - 三次,

他们终于在一半之后来了和小时说对不起,我们没有准备

,我们现在应该准备好。这是非常令人失望的,因为它反复发生了
。我会给这个地方



所以,第一个输出(访问这个地方)很好。

但是第二个不合适,因为它是从单词访问开始输入字符串的一部分,并以这个地方结尾。



我需要输出,比如访问这个地方,参观这个地方,参观过这个地方等等。



因此,对于上面的输入字符串输出应该只有这个

1.访问过这个地方



模式应该是这样的它也会保持单词之间的顺序。



谢谢。



When i console it this gives the output like

1. visited this place
2. visit they failed to deliver the complete order and on asking twice - thrice,
they finally came after half and hour and said that " sorry we didn't prepare
that, shall we prepare it now". It was pretty disappointing as it happened
repeatedly. I would have giving this place

So, The first output(visited this place) is fine.
But the second one not proper because it is taking the part of input string starting from word "visit" and ends with "this place".

I need the output like visit this place,visiting this place,visited this place etc..

So, for the above input string output should be this only
1. visited this place

The pattern should be in this manner so that it will maintain the sequence between the word also.

Thanks.

推荐答案

问题在于访问......这个地方在输入短语中出现两次。所以你有两个选择:

1)之后检查每场比赛并丢弃不正确的



2)限制长度有效输入,以防止第二个匹配。

为此尝试类似:

The problem is that "visit...this place" occurs twice in the input phrase. So you have two options:
1) Check each match afterwards and discard "improper" ones
Or
2) Limit the length of of the "valid" input to prevent the second being a match at all.
For that try something like:
(visit(.{1,10}?)\sthis\splace)

其中10是你接受的最大长度。

Where 10 is the max length you will accept.


这篇关于正则表达式与单词序列维持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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