Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What regEx can I use to Split a string into whole words but only if they start with #?

Tags:

regex

vb.net

I have tried this...

Dim myMatches As String() = 
System.Text.RegularExpressions.Regex.Split(postRow.Item("Post"), "\b\#\b")

But it is splitting all words, I want an array of words that start with#

Thanks!

like image 309
Doug Avatar asked Sep 29 '08 18:09

Doug


2 Answers

This seems to work...

c#

Regex MyRegex = new Regex("\\#\\w+");
MatchCollection ms = MyRegex.Matches(InputText);

or vb.net

Dim MyRegex as Regex = new Regex("\#\w+")
Dim ms as MatchCollection = MyRegex.Matches(InputText)

Given input text of...

"asdfas asdf #asdf asd fas df asd fas #df asd f asdf"

...this will yield....

"#asdf" and "#df"

I'll grant you that this does not get you a string Array but the MatchCollection is enumerable and so might be good enough.


Additionally, I'll add that I got this through the use of Expresso. which appears to be free. It was very helpful in producing the c# which I am very poor at. (Ie it did the escaping for me.) (If anyone thinks I should remove this pseudo-advert then please comment, but I thought it might be helpful :) Good times )

like image 65
Rory Becker Avatar answered Oct 18 '22 13:10

Rory Becker


Since you want to include the words in the split you should use something like

"\b#\w+\b"

This includes the actual word there in the expression. However I'm not sure that's what you want. Could you provide an example of input and the desired output?

like image 44
rslite Avatar answered Oct 18 '22 14:10

rslite