简体   繁体   English

如何在PHP中设置正则表达式

[英]How to set a Regular Expression in PHP

I would like to know how to get the regular expression of the following text: 我想知道如何获取以下文本的正则表达式:

XXXXXX[Some Same Text # AAAA-NNNN]XXXXXX

Where: 哪里:

'X' can vary any text: Alfa, numerical or symbols (The X can have 0 to infinite characters and it is NOT obligatory).
'A' Alfa uppercase text (The Alfa has exactly 4 uppercase letters and it is obligatory).
'N' Numbers (The N have exactly 4 numbers and it is obligatory).
'Some Same text' has exactly the same text always and it is obligatory.
'[,],#,-' will always be there in that same possition and it is obligatory.

I have a preg_match that goes: 我有一个preg_match去:

*[Some Same Text([A-Z]{4})-([0-9]{4})]*

And it isn't seem to be working. 而且它似乎不起作用。

Thanks and regards! 谢谢并恭祝安康!

. matches any character, and * means 0..infinity. 匹配任何字符,并且*表示0..infinity。 When using [ and ] you need to escape them. 使用[]您需要对其进行转义。 Try this: 尝试这个:

.*\[Some Same Text # ([A-Z]{4})-([0-9]{4})\].*

This should work: '/^.*\\[Some Same Text\\s#\\s[AZ]{4}-\\d{4}\\].*$/' . 这应该起作用: '/^.*\\[Some Same Text\\s#\\s[AZ]{4}-\\d{4}\\].*$/' As Tested: 经测试:

if(preg_match('/^.*\[Some Same Text\s#\s[A-Z]{4}-\d{4}\].*$/', 'XXXXXX[Some Same Text # AAAA-4444]XXXXXX'))echo 1;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM