PHP正则表达式和preg

编程入门 行业动态 更新时间:2024-10-23 04:33:39
PHP正则表达式和preg_match(PHP regex and preg_match)

我有一个我需要匹配的字符串,可以是各种格式:

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,11

I 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

enter image description here

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

Examples

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] => 11


Or

You could just look for all numbers which are not followed by an equals sign

\d+(?!=|\d) Live Demo

enter image description here

更多推荐

数字,numbers,无法,需要,电脑培训,计算机培训,IT培训"/> <meta name="description&

本文发布于:2023-07-26 00:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式   PHP   preg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!