简体   繁体   中英

Regular expression to extract email address from another regular expression

I've got a piece of text that contains another regular expression.

Sample text:

...<rege>!^.*$!mailto:asdf@adsfsdaf.com!</rege>...

What I want to match is:

mailto:asdf@adsfsdaf.com

This didn't work:

    $patterns[] = '/<rege>!\^\.\*\$!(.*)!<\/rege>/';

Thoughts?

试试这个:

$patterns[] = '/<rege>!\^\.\*\$!([^<]*)!<\/rege>/';

To have \\ inside '' you have to double it like so:

$patterns[] = '/<rege>!\\^\\.\\*\\$!(.*)!<\\/rege>/';

I also think that you may want this match to be ungreedy because you want to properly capture email even if similar constructs occur in search text multiple times so:

$patterns[] = '/<rege>!\\^\\.\\*\\$!(.*?)!<\\/rege>/';

You need to escape your backslashes:

<?php

$sample = "<rege>!^.*$!mailto:asdf@adsfsdaf.com!</rege>";
preg_match("/<rege>!\\^\\.\\*\\$!(.*)!<\/rege>/",$sample,$matches);

print_r($matches);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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