Regex split for words containing periods

Can someone tell me how to change this regex to indicate periods in a string?

string[] parts = Regex.Split(s, @"\b|[^\.#_a-zA-Z0-9()=><!%]");

If I provide the line: "HELLO ABC.123"

This regular expression returns {"HELLO", "ABC", ".", "123"}

I want to return {"HELLO", "ABC.123"}

Please forgive my noobishness for regex patterns.

EDIT: I am using C # 3.5

+4
source share
2 answers

\b matches on either side of the period in ABC.123 . You can change it to avoid this. For instance:

 (?<![\w.])(?=[\w.])|(?<=[\w.])(?![\w.]) 

Providing the full quoted expression:

 @"(?<![\w.])(?=[\w.])|(?<=[\w.])(?![\w.])|[^\w.#()=><!%]+" 

You can add #()=><!% Characters to all character classes.

+3
source

Just remove \b from \b|[^\.#_a-zA-Z0-9()=><!%] And use:

 string[] parts = Regex.Split(s, @"[^#_a-zA-Z0-9()=><!%]"); 
+1
source

All Articles