我有一个我需要匹配的字符串,可以是各种格式:
5=33 5=14,21 5=34,76,5 5=12,97|4=2 5=35,22|4=31,53,71 5=98,32|7=21,3|8=44,11我需要在等号(=)和管道(|)符号之间出现的数字,或者行的末尾。 所以在最后一个例子中我需要98 ,但我根本无法解决这个问题。 数字不具体,可以是任意数量的数字。
我只是学习正则表达式和preg_match而无法搞清楚。 我不知道我在做什么。
非常感谢任何帮助。
I have a string that I need to match, which can be in various formats:
5=33 5=14,21 5=34,76,5 5=12,97|4=2 5=35,22|4=31,53,71 5=98,32|7=21,3|8=44,11I need the numbers that appear between the equal sign (=) and pipe (|) symbol, or to the end of the line. So in the last example I need 98,32,21,3,44,11 but I can't figure this out at all. The numbers are not concrete, they can be any quantity of numbers.
I am just learning regex and preg_match and can't figure it out. I have no idea what I am doing.
Any help is greatly appreciated.
最满意答案
描述
这个表达式将:
只匹配数字 要求数字在数字后面直接有逗号,垂直管道或字符串结尾,这样可以防止包含等号的数字。\d+(?=[,|\n\r]|\Z) 现场演示
NODE EXPLANATION -------------------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- (?= look ahead to see if there is: -------------------------------------------------------------------------------- [,|\n\r] any character of: ',', '|', '\n' (newline), '\r' (carriage return) -------------------------------------------------------------------------------- | OR -------------------------------------------------------------------------------- \Z before an optional \n, and the end of the string -------------------------------------------------------------------------------- ) end of look-ahead例子
样品
使用此表达式,字符串5=98,32|7=21,3|8=44,11将返回一个字符串数组:
[0] => 98 [1] => 32 [2] => 21 [3] => 3 [4] => 44 [5] => 11要么
您可以查找所有未跟随等号的数字
\d+(?!=|\d) 现场演示
Description
This expression will:
match only numbers requires the numbers to have a comma, vertial pipe, or end of string directly after the number, this prevents the numbers with an equals sign from being included.\d+(?=[,|\n\r]|\Z) Live Demo
NODE EXPLANATION -------------------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- (?= look ahead to see if there is: -------------------------------------------------------------------------------- [,|\n\r] any character of: ',', '|', '\n' (newline), '\r' (carriage return) -------------------------------------------------------------------------------- | OR -------------------------------------------------------------------------------- \Z before an optional \n, and the end of the string -------------------------------------------------------------------------------- ) end of look-aheadExamples
Samples
With this expression the string 5=98,32|7=21,3|8=44,11 will return an array of strings:
[0] => 98 [1] => 32 [2] => 21 [3] => 3 [4] => 44 [5] => 11Or
You could just look for all numbers which are not followed by an equals sign
\d+(?!=|\d) Live Demo
更多推荐
数字,numbers,无法,需要,电脑培训,计算机培训,IT培训"/> <meta name="description&
发布评论