简体   繁体   English

正则表达式和换行符

[英]Regular expression and newline

I have such text: 我有这样的文字:

 <Neednt@email.com> If you do so, please include this problem report. <Anotherneednt@email.com> You can delete your 

own text from the attached returned message. 附加的返回消息中的文本。

  The mail system <Some@Mail.net>: connect to *.net[82.*.86.*]: Connection timed out 

I have to parse email from it. 我必须从中解析电子邮件。 Could you help me with this job? 你能帮我这份工作吗?


upd UPD

There could be another email addresses in <%here%> . <%here%>中可能还有另一个电子邮件地址。 There should be connection between 'The mail system' text. “邮件系统”文本之间应该有联系。 I need in email which goes after that text. 我需要在该文本后面的电子邮件。

Considering this text is stored in $text, what about this : 考虑到此文本存储在$ text中,该如何处理:

$matches = array();
if (preg_match('/<([^>]+)>/', $text, $matches)) {
  var_dump($matches[1]);
}

Which gives me : 这给了我:

string 'Some@Mail.net' (length=13)


Basically, I used a pretty simple regex, that matches : 基本上,我使用了一个非常简单的正则表达式,它匹配:

  • a < character 一个<字符
  • anything that's not a > character : [^>] 任何不是>字符的内容: [^>]
    • at least one time : [^>]+ 至少一次: [^>]+
    • capturing it : ([^>]+) 捕获它: ([^>]+)
  • a > character 一个>字符

So, it captures anything that's between < and > . 因此,它将捕获<>之间的任何内容。


Edit after comments+edit of the OP : 在OP的评论+编辑之后进行编辑:

If you only want the e-mail address that's after The mail system , you could use this : 如果您只想要The mail system之后的电子邮件地址,则可以使用以下命令:

$matches = array();
if (preg_match('/The mail system\s*<([^>]+)>/', $text, $matches)) {
  var_dump($matches[1]);
}

In addition to what I posted before, this expects : 除了我之前发布的内容外,这还期望:

  • The string The mail system 字符串The mail system
  • Any number of white-characters : \\s* 任意数量的白色字符: \\s*

You want to use preg_match() and looking at this input it should be simple: 您想使用preg_match()并查看此输入应该很简单:

<?php

if (preg_match('/<([^>]*?@[^>]*>/', $data, $matches)) {
  var_dump($matches); // specifically look at $matches[1]
}

There are other patterns that would match it, you don't have to stick to that same pattern. 还有其他与之匹配的模式,您不必坚持相同的模式。 The '<' and '>' in your input are helpful here. 输入中的“ <”和“>”在这里很有帮助。

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

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