2017-02-22 14 views
1

私は電子メールからデータを抽出しています。Regex/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) 
関連する問題