查看模式内部是否父模式匹配并在模式之间共享字符
我有一个像这样的字符串:
门票订单: № 123123123. CED-MSW-RPG-MOW-CEK PODYLOVA/ALEMR 555 423578932 2011 年 10 月 19 日门票订购: № 123123123. 346257. CSK-MOW-PRG-MOW-CWQ PODYLOVA/ALEMR 555 45837043 19OCT11
我需要收集所有代码,包括 CEK、MOW、PRG 等。我首先尝试了这种模式:
$pattern = '#[-|\s]([A-Z]{3})#';
结果得到了我的所有代码(没问题)和用户姓氏的前 3 个字符:“PODYLOVA”中的“POD”。如果我说“在我的代码之后必须是连字符或可用空间字符,通过将我的模式更改为:
$pattern = '#[-|\s]([A-Z]{3})[-|\s]#';
我的 $matches var 有这样的:
array (
0 =>
array (
0 => ' CED-',
1 => '-RPG-',
2 => '-CEK ',
3 => ' CSK-',
4 => '-PRG-',
5 => '-CWQ ',
),
1 =>
array (
0 => 'CED',
1 => 'RPG',
2 => 'CEK',
3 => 'CSK',
4 => 'PRG',
5 => 'CWQ',
),
)
您可以看到,我的模式不会在所需代码之间“共享”连字符。
我明白两种解决方案,但无法对模式进行成像,这将适合:
- 使模式在代码之间共享连字符
- 制作更复杂的模式:首先收集包含代码(“CED-MSW-RPG-MOW-CEK”)的文本,然后得到全部#([AZ]{3}# 在这个模式中。
看起来,解决方案#1 在我的情况下是最好的,但它应该是什么样子呢?
I have a string like this:
Tickets order: № 123123123. CED-MSW-RPG-MOW-CEK PODYLOVA/ALEMR 555
423578932 19OCT11 Tickets order: № 123123123. 346257.
CSK-MOW-PRG-MOW-CWQ PODYLOVA/ALEMR 555 45837043 19OCT11
I need to collect all codes that are CEK, MOW, PRG and so on. I tried this pattern firstly:
$pattern = '#[-|\s]([A-Z]{3})#';
As result a get all my codes (that's ok) and the first 3 chars of users surname: "POD" from "PODYLOVA". If i say "after my code must be an hyphen or free space char by changing my pattern to this:
$pattern = '#[-|\s]([A-Z]{3})[-|\s]#';
My $matches var has this:
array (
0 =>
array (
0 => ' CED-',
1 => '-RPG-',
2 => '-CEK ',
3 => ' CSK-',
4 => '-PRG-',
5 => '-CWQ ',
),
1 =>
array (
0 => 'CED',
1 => 'RPG',
2 => 'CEK',
3 => 'CSK',
4 => 'PRG',
5 => 'CWQ',
),
)
You can see, that my pattern doesn't "share" the hyphen between desired codes.
I see two solutions, but cannot imaging the pattern, which will suit:
- Make the pattern to share the hyphen between codes
- Make more complicated pattern: firstly collect the text which contains codes ("CED-MSW-RPG-MOW-CEK") and then get all #([A-Z]{3}# inside this pattern.
It seems, that solution#1 is the best in my case, but how it should look?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
\b([AZ]{3})\b
HTH
Try this:
\b([A-Z]{3})\b
HTH
这能给你你想要的吗?
用 grep 测试:
does this give you what you want?
tested with grep: