英文:
PHP regular expression to find "email":"email@domain.com" pattern
问题
Here's the translated portion of your text:
我有一个函数,可以找到特定格式的电子邮件字符串。我需要在较大的字符串中找到这个特定的电子邮件字符串。我需要找到的特定电子邮件字符串必须具有以下格式:
"email":"email@domain.com"
我需要找到这种类型字符串的任何出现。以下是我用于查找的函数:
function find_email_schema($str){
preg_match_all('/["email":""]+[._a-zA-Z0-9-]+@[._a-zA-Z0-9-]+/i', $str, $matches);
return $matches;
}
然而,这个函数并不按预期工作,它会找到较大字符串中不符合此格式的其他电子邮件:
"email":"email@domain.com"
我只想要以"email"开头的电子邮件:
我知道我传递给preg_match_all的模式不正确,但我不确定需要更改什么才能只获取符合上述格式的电子邮件。我需要在我的正则表达式模式中做哪些修改才能使其正常工作?
这是我正在使用的模式:
/["email":""]+[._a-zA-Z0-9-]+@[._a-zA-Z0-9-]+/i
If you have any further questions or need additional assistance, please feel free to ask.
英文:
I have a function that will find an email string that is in a specific format. I need to find this specific email string within the larger string. The specific email string I need to find has to be in this format:
"email":"email@domain.com"
I need to find any occurrence of this type of string. Here is my function to find that:
function find_email_schema($str){
preg_match_all('/["email":"]+[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i', $str, $matches);
return $matches;
}
However, this function isn't working as expected, it finds other emails in the larger string that aren't in this format:
"email":"email@domain.com"
I only want emails that start with "email":
I know that the pattern I'm passing to preg_match_all isn't correct, but I'm not sure what I need to change to only obtain emails that comply with the above format. What do I need to alter in my regex pattern to get this working?
This is the pattern I'm using:
/["email":"]+[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i
答案1
得分: 3
Because the "email":
part is literal, you don't need to enclose it between [
]
, so try this one (I also add the email address enclosing "
optional with the ?
):
"email":"?[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+"?
[
]
are used to declare character ranges like a-z
indicating from a
to z
, like you did for the next part.
Let's try it on regex101.com:
英文:
Because the "email":
part is literal, you don't need to enclose it between [
]
, so try this one (I also add the email address enclosing "
optional with the ?
):
/"email":"?[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+"?/i
[
]
are used to declare character ranges like a-z
indicating from a
to z
, like you did for the next part.
Let's try it on regex101.com:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论