ChatGPT解决这个技术问题 Extra ChatGPT

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

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.


m
mmx
[^,;]+         

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:

[,;]+

And the question doesn't specify whether adjacent separators are allowed, so the trailing '+' is slightly dubious.
Getting an error only for semicolon-- unterminated regexp meets end of file
I had a similar requirement where I want to avoid semicolon and comma at the end I tried a lot but no success below is the Regex I am using const regexDomain = /^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9]/g; Well it validates if I use , and ; in between but fails at the end to vliadate.
T
Thom Smith

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

[^,;]

I had a similar requirement where I want to avoid semicolon and comma at the end I tried a lot but no success below is the Regex I am using const regexDomain = /^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9]/g; Well it validates if I use , and ; in between but fails at the end to vliadate.
B
BigRon

use a negative character class:

[^,;]+

N
NawaMan

Use this:

([^,;]*[,;])*

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.
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.
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

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now