Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

regex - Regular expression help - comma delimited string

I don't write many regular expressions so I'm going to need some help on the one.

I need a regular expression that can validate that a string is an alphanumeric comma delimited string.

Examples:

  • 123, 4A67, GGG, 767 would be valid.
  • 12333, 78787&*, GH778 would be invalid
  • fghkjhfdg8797< would be invalid

This is what I have so far, but isn't quite right: ^(?=.*[a-zA-Z0-9][,]).*$

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sounds like you need an expression like this:

[0-9a-zA-Z]+(,[0-9a-zA-Z]+)*

Posix allows for the more self-descriptive version:

[[:alnum:]]+(,[[:alnum:]]+)*
[[:alnum:]]+([[:space:]]*,[[:space:]]*[[:alnum:]]+)*  // allow whitespace

If you're willing to admit underscores, too, search for entire words (w+):

w+(,w+)*
w+(s*,s*w+)*  // allow whitespaces around the comma

(Thanks to Alan for pointing out several of my failings!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...