简体   繁体   English

Regex / preg_match-在字符串和电子邮件地址之间获取文本

[英]Regex/preg_match - get text between string and email address

I'm extracting data from emails. 我正在从电子邮件中提取数据。 I have pieces of text like this: 我有几段这样的文字:

Eg. 1: some standard text.   Bugs Bunny bugs@gmail.com 0411111111 more standard text 
Eg. 2: some standard text.   Bugs The Bunny bugs@gmail.com 0411111111 more standard text
Eg. 3: some standard text.   Bugs Bunny bugs.bunny@gmail.com 0411111111 more standard text
Eg. 4: some standard text.   Bugs bugs.bunny@gmail.com +6141 111 111 more standard text

As you can see, there is a name, email and phone number that I want to extract. 如您所见,有一个我要提取的姓名,电子邮件和电话号码。 The email should be easy enough, and I'm sure I can work out the phone options but how could I get the name? 电子邮件应该很容易,我敢肯定我可以算出电话选项,但是我怎么能得到名字呢?

I know the logic is: get the text after some standard text. 我知道逻辑是:在some standard text.之后获取some standard text. and before the the first non-space-separated string before the @ , but how? 并且在@之前的第一个非空格分隔的字符串之前,但是如何?

This is my starting point (?<=some standard text. )(.*?)(?=@) 这是我的出发点(?<=some standard text. )(.*?)(?=@)

This gives me a result with a group (?<=some standard text. )(.*?)(?:[\\w-\\.]+)@ so I think I'm on the right path. 这给了我一个带有分组(?<=some standard text. )(.*?)(?:[\\w-\\.]+)@所以我认为我走对了。

I'm using php. 我正在使用php。

Here is a quick version/example I came up with: 这是我想到的一个快速版本/示例:

(?<=some standard text. )(.*?) ([^\s]+@[^\s]+) (\+?\d+(?:\s\d+)*) 

regex101.com/r/Wjz66g/1 regex101.com/r/Wjz66g/1

It's not perfect, but it does follow along the same lines as what you were doing and might work enough. 它不是完美的,但确实遵循与您正在做的事情相同的路线,并且可能会足够工作。

I wrote this, you can test it on: https://regex101.com/r/A29hjE/8 我写了这个,可以在以下网址测试: https : //regex101.com/r/A29hjE/8

(?x) # Here we are entering the the free space mode

# Here we assure the spaces are not matched by the `[\w ]+` group
(?:\.\s+)

# Here we are matching for the guys name, before its email address
([\w ]+(?:\w+))\s+

# Here we match the email
(\w[^\s]+@[^\s]+)\s+

# Here  we match the telephone number
(\+?[\d ]+)(?!\w)

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

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