正则表达式帮助 - 逗号分隔的字符串 [英] Regular expression help - comma delimited string

查看:78
本文介绍了正则表达式帮助 - 逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不会写很多正则表达式,所以我需要一些帮助.

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.

示例:

  • 123, 4A67, GGG, 767 将是有效的.
  • 12333, 78787&*, GH778 将无效
  • fghkjhfdg8797< 将无效
  • 123, 4A67, GGG, 767 would be valid.
  • 12333, 78787&*, GH778 would be invalid
  • fghkjhfdg8797< would be invalid

这是我目前所拥有的,但不太正确:^(?=.*[a-zA-Z0-9][,]).*$

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

有什么建议吗?

推荐答案

听起来您需要这样的表达式:

Sounds like you need an expression like this:

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

Posix 允许使用更具自我描述性的版本:

Posix allows for the more self-descriptive version:

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

如果您也愿意接受下划线,请搜索整个单词 (\w+):

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

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

(感谢 Alan 指出我的几个错误!)

这篇关于正则表达式帮助 - 逗号分隔的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆