Regex search for two words seperated by a space  
Author Message
B Langston





PostPosted: .NET Base Class Library, Regex search for two words seperated by a space Top

I have a text file that I need to match on. I load the file into a string and then strip out the newline characters (\012’s) and replace them with stars (\052’s) to make working with them simpler. So then I have a string variable made up of groups of text separated by double stars (**) with the occasional star in-between the double stars. I need to find names within those ‘groups’ but I also need all the text within the group.

I build my regex by using a string variable for the name and end up with a regex like this: Dim textForRegex As String = "( xism)\*\*[^\*]*" & strName

This works as long as I use a single name in my ‘Prompt’ string. When I type in two words such as ‘John Smith’ my regex search fails. Can anyone point out what I am doing wrong

Thank you very much

Bill Langston




.NET Development35  
 
 
cgraus





PostPosted: .NET Base Class Library, Regex search for two words seperated by a space Top

Spaces are ignored in the regex, replace the space with [\W*] first.

www.codeproject.com has a program called Expresso, if you google Expresso regex, you'll find a lot of sites have it. It's invaluable for building regex.



 
 
B Langston





PostPosted: .NET Base Class Library, Regex search for two words seperated by a space Top

Thank you Mr. Graus.

Can you elaborate as to what you mean by replacing the spaces That is, within the context of my regex, I'm not sure what you are saying.

Bill Langston



 
 
cgraus





PostPosted: .NET Base Class Library, Regex search for two words seperated by a space Top

\W is what a regex uses for detecting space. So, you need to replace the spaces in your search string with \W so the regex understands what you want. [\W*] just finds a group of whitespace.



 
 
B Langston





PostPosted: .NET Base Class Library, Regex search for two words seperated by a space Top

Okay, I think I follow you. I take my string variable that I get from my user Prompt and rebuild it to replace the spaces in that name with '\W', then add that to my regex. Is that correct

 
 
cgraus





PostPosted: .NET Base Class Library, Regex search for two words seperated by a space Top

Yes, that would give the most exact match, if each space is replaced by \W.



 
 
B Langston





PostPosted: .NET Base Class Library, Regex search for two words seperated by a space Top

Okay, Mr. Graus, I can do that. I waited to hear back from you that my assumption was correct before I tried it out.

I really hoping that this works because I have beat my head against the wall of regex 'syntax' so hard that I've got a headache!

I'll try this out first with Expresso which I already have, by just hardcoding a made up name and using those \W's and thanks again.



 
 
cgraus





PostPosted: .NET Base Class Library, Regex search for two words seperated by a space Top

Glad to help :-)

Yes, Expresso rocks, I always use it when I answer a regex question, or do my own regex work.



 
 
Kareem Shaker





PostPosted: .NET Base Class Library, Regex search for two words seperated by a space Top

Hi,

I couldn't get all what you want to do, but I can see that you do replace some lines before processing your string using Regex,

Look based on my experience with Regex, herein the best way to ask a question about Regex, please provide me with the following and I will send you the regex you need

- Input String.

- Match Result Example.

- Brief Description for matching criteria.

Also, you should try Expresso it's definitely a must, also you should have a look at Regulator it also rocks !

Regards,
http://CairoCafe.BlogSpot.com



 
 
B Langston





PostPosted: .NET Base Class Library, Regex search for two words seperated by a space Top

Thank you sir for your offer to help but I have solved my problem now.

I found that by replacing the characters that were giving me trouble, like linefeeds, with characters that I could type made my building the regex easy.

Another valuable tip I got was to replace spaces with "\W" when building your regex; that is if you are building your regex by using concatenation and one of the 'units' you are concatenating is a string variable with whitespace in it, replace the spaces with the "\W".

Also, thanks for the tip about 'Regulator', I will go look for it right now.

yours,

Bill Langston