-1

我想知道如何获取以下文本的正则表达式:

XXXXXX[Some Same Text # AAAA-NNNN]XXXXXX

在哪里:

'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.

我有一个 preg_match :

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

它似乎没有工作。

谢谢并恭祝安康!

4

2 回答 2

2

.匹配任何字符,*表示 0..infinity。使用时[]您需要逃避它们。尝试这个:

.*\[Some Same Text # ([A-Z]{4})-([0-9]{4})\].*
于 2013-06-14T22:32:27.620 回答
1

这应该工作:'/^.*\[Some Same Text\s#\s[A-Z]{4}-\d{4}\].*$/'. 经测试:

if(preg_match('/^.*\[Some Same Text\s#\s[A-Z]{4}-\d{4}\].*$/', 'XXXXXX[Some Same Text # AAAA-4444]XXXXXX'))echo 1;
于 2013-06-14T22:45:33.597 回答