将正则表达式模式指定为数组的键
我有一个正则表达式数组,并尝试循环遍历文本文档以查找第一个模式,将其指定为数组的键,然后继续查找第二个模式并将其指定为值。每当我遇到模式 1 时,我希望它始终被分配为键,并且在遇到新键之前所有模式 2 匹配都将被分配给第一个键作为值。
文本文档结构:
Subject: sometext
Email: [email protected]
source: www.google.com www.stackoverflow.com www.reddit.com
所以我有一个表达式数组:
$expressions=array(
'email'=>'(\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b)',
'url'=>'([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)'
);
我想循环遍历我的文本文档并匹配电子邮件地址,然后将其指定为数组的键,然后将后面的所有网址指定为值,将输出发送到上面文本将是:
array(
'[email protected]' => array (
0 => 'www.google.com',
1 => 'www.stackoverflow.com',
2 => 'www.reddit.com'
)
I have an array of regular expressions and am trying to loop through a text document to find the first pattern, assign that as the key to an array then continue through find the second pattern and assign that as the value. Whenever I come across pattern 1 I want that to always be assigned as a key and all pattern 2 matches that follow until I come across a new key will be assigned to that first key as values.
Text document structure:
Subject: sometext
Email: [email protected]
source: www.google.com www.stackoverflow.com www.reddit.com
So I have an array of expressions:
$expressions=array(
'email'=>'(\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b)',
'url'=>'([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)'
);
I want to loop through my text document and match the email address then assign that as the key to an array then assign all urls that follow as the values, s the output to the above text would be:
array(
'[email protected]' => array (
0 => 'www.google.com',
1 => 'www.stackoverflow.com',
2 => 'www.reddit.com'
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行此操作的一种方法是:
将
emailexpr
和domainexpr
替换为您的正则表达式乱码。One way to do such a thing:
replace
emailexpr
anddomainexpr
with your regexp gibberish.我会这样做:
I would do: