tags:

views:

1124

answers:

5

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!

+1  A: 

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?

rslite
+4  A: 

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 )

Rory Becker
+1  A: 

Using PHP's preg_match_all():

$text = 'Test #string testing #my test #text.';
preg_match_all('/#[\S]+/', $text, $matches);

echo '<pre>';
print_r($matches[0]);
echo '</pre>';

Outputs:

Array
(
    [0] => #string
    [1] => #my
    [2] => #text.
)
Joe Lencioni
A: 

Here is a code in Javascript:

text = "what #regEx can I use to #split a #string into whole words but only" 
// what #regEx can I use to #split a #string into whole words but only"
text.match(/#\w+/g);
// [#regEx,#split,#string]
J.F. Sebastian
A: 

In Microsoft regex flavours, Try: "\#:w"

That worked for me using Find in Visual Studio. I don't know how it will translate into the Regex class.

EDIT: Backslash was swallowed.

chris