如何将字符串与正则表达式进行比较 [英] How can I compare a string with Regular Expression

查看:348
本文介绍了如何将字符串与正则表达式进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前RegEx.Match()函数有助于将正则表达式与字符串进行比较。但我发现很难做到相反(比较字符串与正则表达式)。



这是可能的(比较正则表达式与字符串)

Present RegEx.Match() function helps to compare regular expression with a string. But I am finding difficult to do vice versa (compare string with regular expression).

This is possible (Compare Regular Expression with String)

Dim match As Match = Regex.Match("Give me your email address?",".* email address .*")

If match.Success Then
  Console.WriteLine(match.Value)
End If





但反之亦然(比较字符串与正则表达式)



But vice versa is not possible (Compare String with Regular Expression)

Dim match As Match = Regex.Match(".* email address .*","Give me your email address?")

If match.Success Then
  Console.WriteLine(match.Value)
End If





我正在尝试创建一个可以匹配模式并为其提供答案的程序....



这是我的主文件。 ..



I am trying to make a program which can match a pattern and provide an answer for it....

This is my main file...

<question>What is your name?</question>
<answer>My name is XYZ</answer>

<question>.*email address.*</question>
<answer>abc@gmail.com</answer> 





现在,如果用户要求给我你的电子邮件地址。所以我想在这里比较我的文件中是否存在这样的问题。



意味着我的主文件(字符串)本身包含通配符。那么如何将用户输入与文件匹配?



那么可能吗?有谁可以帮助我?



您的早期回复将不胜感激。



提前致谢。



Now if user asks give me your email address. So here i want to compare whether such kind of question exists in my file.

Means my main file (string) itself contains wild cards. So how can I match user input with file?

So is it possible? Can anyone please help me?

Your early response will be appreciated.

Thanks in advance.

推荐答案

想想你在问什么:



简单来说,正则表达式命令问字符串是什么x匹配模式y



如果字符串x = abcdefg且模式为。* cd。*然后我们就可以得到一个匹配。



反向是问字符串是否匹配模式x



如果字符串y = 。* cd。*且模式为abcdefg那么我们就不会得到一场比赛。







那么为什么你期望字符串。*电子邮件地址。*匹配模式给我你的电子邮件地址?



你现在看到这个问题了吗?这就是为什么Asif想要知道为什么你问:这个问题没有意义。





尝试描述为什么你要交换条款和想要实现的目标。



希望这有帮助

Andy
think of what you are asking:

in simple terms, Regex commands ask "does the string x match pattern y"

if the string x = "abcdefg" and the pattern is ".*cd.*" then we can get a match.

in inverse is to ask "does the string y match the pattern x"

if the string y = ".*cd.*" and the pattern is "abcdefg" then we can wont get a match.



So why would you expect the string ".* email address .*" to match the pattern "Give me your email address?"?

Do you see the issue, now? That is why Asif wanted to know why you asked: The question does not make sense.


Try describing why you want to swap terms and what you want to acheive.

Hope that helps
Andy


啊 - 好多了。我得到你的要求。



你仍然有这样的问题:字符串x匹配模式y,只有在你想要找到y给定x的情况下。





语言匹配很棘手。关于这一主题有许多同行评审的论文。阿兰图灵提供了着名的图灵测试,即使是最基本的水平也从未传递过。只要问Siri零除零是^ _ ^







你似乎有个主意你想要怎么做,我可以看看你想要做什么。



只有一种方法可以测试多个模式对一个字符串来找到哪个模式使比赛。这将使用模式组。



解释起来很复杂,但实现起来并不复杂。



看到有关正则表达式模式组如何工作的链接:

https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.getgroupnames.aspx [ ^ ]





所以你最终会得到一个巨大的正则表达式:

Ah - much better. I get what you are asking.

You still have the question: "does string x match pattern y", only in your case you want to find y given x.


Language matches are tricky. There are many peer reviewed papers on the subject. There is the famous Turing test proffered by Alan Turing that has never been passed on even the most basic level. Just ask Siri what zero divided by zero is ^_^



You seem to have an idea of how you want to approach this, and I can see what you are trying to do.

There is only 1 way to test multiple patterns against a string to find which pattern made the match. This would be using pattern groups.

It's complex to explain, but not nearly as complex to implement.

see this link on how regex pattern groups work:
https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.getgroupnames.aspx[^]


So you would end up with a huge regex pattern a bit like:
string pattern = @".*(?<email>(email address))(?<howru>(how (are|r)\W?(you|u))).*";
</howru></email>





您可以从正则表达式列表中构建这个,这很好。然后,您可以遍历匹配并检查匹配组名称。



我希望这足以让你入门。 ^ _ ^

Andy



You could probably construct this from a list of regex terms, which would be good. You can then iterate through the matches and check the match group name.

I hope that's enough to get you started. ^_^
Andy


这篇关于如何将字符串与正则表达式进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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