Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What regex will match every character except comma ',' or semi-colon ';'?

Tags:

regex

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.

like image 321
KJ Saxena Avatar asked Sep 11 '09 05:09

KJ Saxena


People also ask

How do you match a character except in regex?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).

What regex matches any character?

Matching a Single Character Using Regex By default, the '. ' dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.

Is comma a special character in regex?

The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ; . The closing ] indicates the end of the character set. The plus + indicates that one or more of the "previous item" must be present.

Is semicolon a special character in regex?

Semicolon is not in RegEx standard escape characters. It can be used normally in regular expressions, but it has a different function in HES so it cannot be used in expressions. As a workaround, use the regular expression standard of ASCII.


4 Answers

[^,;]+         

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:

[,;]+
like image 105
mmx Avatar answered Oct 20 '22 15:10

mmx


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

[^,;]
like image 40
Thom Smith Avatar answered Oct 20 '22 15:10

Thom Smith


use a negative character class:

[^,;]+
like image 42
knittl Avatar answered Oct 20 '22 15:10

knittl


Use this:

([^,;]*[,;])*
like image 43
NawaMan Avatar answered Oct 20 '22 15:10

NawaMan