tags:

views:

568

answers:

4

Is it possible to define a regex which will match every character except a certain defined character or set of characters?

Basically, I wanted to split a string by either comma (,) or semi-colon (;). So I was thinking of doing it with a regex which would match everything until it encountered a comma or a semi-colon.

+3  A: 
[^,;]+

You haven't specified the regex implementation you are using. Most of them have a Split method that takes delimiters and split by them. You might want to use that one with a "normal" (without ^) character class:

[,;]+
Mehrdad Afshari
You missed the semicolon.
Vinay Sajip
And the question doesn't specify whether adjacent separators are allowed, so the trailing '+' is slightly dubious.
Jonathan Leffler
+1  A: 

Use character classes. A character class beginning with caret will match anything not in the class.

[^,;]
Thom Smith
A: 

Use this:

([^,;]*[,;])*
NawaMan
That requires the comma or semi-colon as a field delimiter, rather than as a field separator. The difference matters at the end of a 'line' (or other scanned record structure); typically, you don't want to insist on a comma or semi-colon after the last field. If your regex engine is powerful enough, you can use '`(?:([^,;]*)(?:[^,;]|$))`' (PCRE with non-capturing parentheses). The alternatives of a comma or semi-colon after the field, or end of record, makes things work better. Also consider whether empty fields are allowed.
Jonathan Leffler
Finally, you have to worry about what is actually returned by the captures - did you really want the separators included, and if there are 10 fields on a line, how many of them are returned by the capture notation.
Jonathan Leffler
You are right about all that but the reason I didn't concert those thing in my answer is that I do not know what language/library of RegEx the questioner is asking. He may be using "GREP". Anyway, I appreciate you adding those comments to clear things up for him. :D
NawaMan
A: 

use a negative character class:

[^.,;]+
knittl