PHP正则表达式,用于查找字符串是否使用逗号和分号格式化
我需要确定字符串是否按以下方式格式化:PHP正则表达式,用于查找字符串是否使用逗号和分号格式化,php,regex,string,Php,Regex,String,我需要确定字符串是否按以下方式格式化: text1,x%;text2,y% 因此,这些字符串必须有效: example, 10%; test, 22%; another, 90% another example, 11.5%; yet other example, 91% another, 11%; other example, 11.2% example2, 20% 以下内容必须无效: example3; 10%, test3, 22%; another3, 90% example
text1,x%;text2,y%
因此,这些字符串必须有效:
example, 10%; test, 22%; another, 90%
another example, 11.5%; yet other example, 91%
another, 11%; other example, 11.2%
example2, 20%
以下内容必须无效:
example3; 10%, test3, 22%; another3, 90%
example4; 20%
example5, 11%; test 5, 123%
another fail example, 11,5%; yet other example, 91%
我知道一些正则表达式,但这个验证我甚至不知道如何开始
编辑:添加了更多示例。您可以使用以下正则表达式:
^[\w\s]+,\s*\d+(?:\.\d+)?%(?:;\s*[\p{L}\s]+,\s*\d+(?:\.\d+)?%)*$
看
正则表达式解释:
基本上,它由两部分组成,一部分在之前
和其余可选序列
-字符串的开头^
-1个或多个字母数字或空格字符,后跟[\w\s]+
-一个逗号,然后,
-可选空白\s*
-一个浮点数或整数(由于\d+(?:\。\d+)%
,小数部分是可选的)和一个百分号?
-匹配0个或多个。。。(?:;\s*[\p{L}\s]+,\s*\d+(?:\。\d+?%)*
-后跟可选空格的分号\s*
-1个或多个字母([\p{L}\s]+
)或空格\p{L}
-逗号后跟可选空格,\s*
-浮点数或整数(小数部分可选)和百分号\d+(?:\。\d+)%
-字符串结尾$
任何东西,任何东西代码>?您似乎有冲突<代码>示例2,20%
通过,示例4;20%
没有。@chris85这是一个名字和这个人将获得的利润百分比。sln不,这不是冲突。如果只有一个人,百分比仍然需要用逗号分隔,而不是分号。实际上我的意思是这通过了example2,20%
,但这example5,11%;测试5,123%
没有。你应该准确描述Text
和x
%是由什么组成的。猜谜游戏。但是,我学到了一些东西。非常感谢。非常感谢,但我需要道歉,因为我的问题中没有包含足够的示例,对不起。如果这个正则表达式对您有效,我将只提供解释-它很长,并且我不确定第二部分中的[\p{L}\s]+
。