Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)
VB
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)
VB
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...
XML
<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.
Posted
Updated 14-Jul-15 23:56pm
v4
Comments
_Asif_ 15-Jul-15 4:51am    
Question is not clear, please add more explanation!
Saqib Ghatte 15-Jul-15 5:17am    
I have edited the question and made it simpler. Hope it is understandable now.

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
 
Share this answer
 
v2
Comments
Saqib Ghatte 15-Jul-15 5:57am    
Sorry for improper explanation. Kindly read the question again. I have again modified it.
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:
C#
string pattern = @".*(?<email>(email address))(?<howru>(how (are|r)\W?(you|u))).*";
</howru></email>


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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900