Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - Get all words that are not wrapped with a "/"

Tags:

string

c#

regex

Im really trying to learn regex so here it goes.

I would really like to get all words in a string which do not have a "/" on either side. For example, I need to do this to: "Hello Great /World/" I need to have the results: "Hello" "Great"

is this possible in regex, if so, how do I do it? I think i would like the results to be stored in a string array :)

Thank you

like image 701
user1290653 Avatar asked Nov 29 '25 04:11

user1290653


1 Answers

Just use this regular expression \b(?<!/)\w+(?!/)\b:

var str = "Hello Great /World/ /I/ am great too";
var words = Regex.Matches(str, @"\b(?<!/)\w+(?!/)\b")
    .Cast<Match>()
    .Select(m=>m.Value)
    .ToArray();

This will get you:

Hello
Great
am
great
too
like image 179
Oleks Avatar answered Dec 01 '25 18:12

Oleks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!