2017-02-22 14 views
1

我從電子郵件中提取數據。我有這樣的文字:正則表達式/ preg_match - 獲取字符串和電子郵件地址之間的文本

Eg. 1: some standard text. Bugs Bunny [email protected] 0411111111 more standard text 
Eg. 2: some standard text. Bugs The Bunny [email protected] 0411111111 more standard text 
Eg. 3: some standard text. Bugs Bunny [email protected] 0411111111 more standard text 
Eg. 4: some standard text. Bugs [email protected] +6141 111 111 more standard text 

正如你所看到的,我想提取一個名字,電子郵件和電話號碼。 電子郵件應該很簡單,我確信我可以制定電話選項,但我怎麼能得到這個名字?

我知道的邏輯是:得到some standard text.之後的文字和之前的第一個非空格分隔字符串@之前,但是怎麼樣?

這是我的出發點(?<=some standard text. )(.*?)([email protected])

這給了我一個結果,與一羣(?<=some standard text. )(.*?)(?:[\w-\.]+)@所以我覺得我在正確的道路上。

我正在使用php。

+0

1.你是什麼意思用'全match'做? 2.那麼'一些標準文本.'總是相同的並且總是以點結束? – user

+0

這裏是一個快速版本/我想出的例子:'(?<=一些標準文本。)(。*?)([^ \ s] + @ [^ \ s] +)(\ +?\ d +( ?:\ s \ d +)*)'(https://regex101.com/r/Wjz66g/1)。這並不完美,但它的確遵循了你所做的和可能足夠的工作。 –

+0

@addons_zz - 我剛剛對羣體進行了自我教育,因此我將稍微修改這個問題。 – Warren

回答

2

下面是一個簡單的版本/例如,我想出了:

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

regex101.com/r/Wjz66g/1

它並不完美,但它確實沿着相同的路線爲何種後續你在做,可能工作得不夠。

0

我寫了這一點,你可以測試它: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][email protected][^\s]+)\s+ 

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