简体   繁体   English

PHP 正则表达式匹配字符串中的所有单个字母后跟数字值

[英]PHP regex to match all single letters followed by numeric value in string

I'm trying to get the a regex running for the following type of strings: one upper case letter followed by a numeric value.我正在尝试为以下类型的字符串运行正则表达式:一个大写字母后跟一个数值。 The string can consist of multiple of these letter-numeric-value combinations.该字符串可以由多个这些字母-数字-值组合组成。 Here some examples and my expected output:这里有一些例子和我预期的 output:

A12B8Y9CC10
-> output [0 => 12, 1 => 8, 2 => 9] (10 is ignored, because there are two letters)
V5C8I17
-> output [0 => 5, 1 => 8, 2 => 17]
KK18II9
-> output [] (because KK and II are always two letters followed by numeric values)
I8VV22ZZ4S9U2
-> output [0 => 8, 1 => 9, 2 => 2] (VV and ZZ are ignored)
A18Z12I
-> output [0 => 18, 1 => 12] (I is ignored, because no numeric value follows)

I tried to reach this by the following regex using preg_match: /^([AZ]{1}\d{1,)$/我尝试使用 preg_match 通过以下正则表达式来达到此目的: /^([AZ]{1}\d{1,)$/

But it doesn't give the expected output.但它没有给出预期的 output。 Can you please help me, how to solve this?你能帮我吗,如何解决这个问题?

Thanks and best regards!谢谢和最好的问候!

You may use this regex in php using preg_match_all :您可以使用preg_match_allphp中使用此正则表达式:

preg_match_all('/(?<![a-zA-Z])[a-zA-Z]\K\d+/', $string, $matches);

Resulting in array $matches[0] to return all the matches.导致数组$matches[0]返回所有匹配项。

RegEx Demo正则表达式演示

RegEx Details:正则表达式详细信息:

  • (?<![a-zA-Z]) : Make sure we don't have a letter before current position (?<![a-zA-Z]) :确保我们在当前 position 之前没有字母
  • [a-zA-Z] : Match a letter [a-zA-Z] : 匹配一个字母
  • \K : Reset match info \K : 重置比赛信息
  • \d+ : Match 1+ digits \d+ :匹配 1+ 个数字

Another variant could be using SKIP FAIL to skip matches that do not qualify.另一种变体可能是使用SKIP FAIL来跳过不符合条件的匹配项。

[A-Z]{2,}\d+(*SKIP)(*FAIL)|[A-Z](\d+)

Explanation解释

  • [AZ]{2,}\d+ Match 2 or more uppercase chars AZ and 1+ digits [AZ]{2,}\d+匹配 2 个或多个大写字符 AZ 和 1+ 个数字
  • (*SKIP)(*FAIL) Use SKIP FAIL to avoid matching (*SKIP)(*FAIL)使用 SKIP FAIL 避免匹配
  • | Or或者
  • [AZ](\d+) Match a single char AZ and capture in group 1 one or more digits [AZ](\d+)匹配单个字符 AZ 并在第 1 组中捕获一位或多位数字

Regex demo |正则表达式演示| Php demo Php 演示

The matches are the first capturing group.匹配是第一个捕获组。

$pattern = '/[A-Z]{2,}\d+(*SKIP)(*FAIL)|[A-Z](\d+)/';
preg_match_all($pattern, $string, $matches);
print_r($matches[1]);

Or with the \K as in anubhava's answer或者在anubhava的答案中使用\K

[A-Z]{2,}\d+(*SKIP)(*FAIL)|[A-Z]\K\d+

Regex demo |正则表达式演示| php demo php 演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式PHP-不匹配特定的字符串,后跟数字 - Regex PHP - dont match specific string followed by numeric 我如何使用正则表达式来检查具有两个固定大写字母的值,后跟PHP或jQuery中的数值? - How would I use regex to check for a value that has two fixed uppercase letters followed by numeric values in PHP or jQuery? PHP Regex匹配,匹配以数字开头和后跟句点的所有行 - PHP Regex match, match all lines that begin with number followed by period PHP Regex匹配多个字符串组合insinde一个字符串并删除所有匹配之间不需要的字母 - PHP Regex match multiple string combinations insinde a string and remove unwanted letters between all matches PHP正则表达式匹配包含下划线或后跟数字的字符串 - Php regex to match string containing underscore or followed by a digit 正则表达式匹配一个数字,后跟一个“。”,再匹配另一个数字 - RegEx to match a single digit followed by a “.” followed by another single digit 用于字母后跟数字后跟“hotmail.com”的 PHP 正则表达式 - PHP regex for letters followed by numbers followed by "hotmail.com" PHP Regex:在数字值上拆分字符串 - PHP Regex: split a string on numeric value PHP正则表达式匹配所有大写字母中的单词或单词组 - PHP Regex match word or group of words in all capital letters 正则表达式匹配字符串php中的任何单个字母 - Regex to match any single alphabet in string php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM