简体   繁体   English

PHP正则表达式匹配确切的字符串

[英]PHP regular expression to match exact string

I have string like "ABC 1000", "ABC 1", "ABC 100". 我有类似“ ABC 1000”,“ ABC 1”,“ ABC 100”的字符串。

In above string example, first 3 character ie ABC is fixed every time and then digits, digits can be long upto N numbers. 在上面的字符串示例中,前3个字符(即ABC)每次都是固定的,然后是数字,数字可以长到N个数字。

In 2nd part ie after "ABC " it should always be numberic value, no alphabet, no special symbol. 在第二部分,即“ ABC”之后,它应始终为数字值,无字母,无特殊符号。

So, how can I manage with regular expression. 因此,如何使用正则表达式进行管理。 Please help. 请帮忙。

I have tried with following but failed .. 我尝试了以下但失败了..

$var="ABC 100";

preg_match("/^INR /[0-9]+/", $var)

You have an extra / in your regular expression. 您的正则表达式中有一个多余的/ It should be: 它应该是:

preg_match('/^ABC \d+/', $var);

You could use this: 您可以使用此:

^[A-Za-z]{3} *\d+$

http://regex101.com/r/gE4mS4 http://regex101.com/r/gE4mS4

$var="ABC 100";
preg_match("/^[A-Za-z]{3} *\d+$/", $var)
  • 3 letters (case insensitive) 3个字母(不区分大小写)
  • 0 or more space 0个或更多空间
  • 1 or more digits 1个或多个数字

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

相关问题 创建正则表达式以空格和字符串长度8匹配确切的单词 - Create regular expression match exact word with space followed by string length 8 PHP:简单的正则表达式以匹配数字的确切长度 - PHP: Simple regular expression to match exact length of digits php正则表达式,如果不在HTML标记中,则匹配字符串 - php regular expression to match string if NOT in an HTML tag 正则表达式匹配php - regular expression match php PHP特殊正则表达式模式,用于在特定字符串后匹配URL - PHP special regular expression pattern to match URLs after a specific string php正则表达式匹配prbl关于以一个字符串开头而不是另一个字符串 - php regular expression match prbl about starting with one string but not the other 使用preg_match和正则表达式在php中进行字符串过滤 - String filtering in php with preg_match and regular expression 使用php和javascript匹配字符串中最后一个整数的正则表达式 - Regular expression to match the last integer in a string with php and javascript PHP preg_match正则表达式,用于在字符串中查找日期 - PHP preg_match regular expression for find date in string 如果存在匹配项,则PHP正则表达式将替换字符串的开头和结尾 - PHP regular expression replace beginning and end of string if match exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM