正则表达式中的大于和小于符号 [英] Greater than and less than symbol in regular expressions

查看:300
本文介绍了正则表达式中的大于和小于符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是正则表达式的新手,我只是因为真正研究了所有正则表达式字符而感到疲倦.我需要知道正则表达式中大于符号的目的是什么,例如:

I am new to regular expressions, and I am just tired by really studying all of the regex charatcer and all. I need to know what is the purpose of greater than symbol in regex for eg:

preg_match('/(?<=<).*?(?=>)/', 'sadfas<email@email.com>', $email);

请告诉我正则表达式中大于号和小于号的用法.

Please tell me the use of greater than symbo and less than symbol in regex.

推荐答案

大于号只是匹配目标字符串末尾的文字 >.

The greater than symbol simply matches the literal > at the end of your target string.

小于符号不是那么简单.首先让我们回顾一下环视语法:

The less than symbol is not so simple. First let's review the lookaround syntax:

模式 (?<={pattern}) 是一个肯定的后视断言,它测试当前匹配的字符串前面是否有匹配 {pattern} 的字符串.

The pattern (?<={pattern}) is a positive lookbehind assertion, it tests whether the currently matched string is preceded by a string matching {pattern}.

模式 (?={pattern}) 是一个积极的前瞻断言,它测试当前匹配的字符串后面是否跟有匹配 {pattern} 的字符串.

The pattern (?={pattern}) is a positive lookahead assertion, it tests whether the currently matched string is followed by a string matching {pattern}.

所以打破你的表达

  • (?<=<) 断言当前匹配的字符串前面是文字 <
  • .*? 匹配任何东西零次或多次,懒惰
  • (?=>) 断言当前匹配的字符串后跟文字 >
  • (?<=<) assert that the currently matched string is preceded by a literal <
  • .*? match anything zero or more times, lazily
  • (?=>) assert than the currently matched string is followed by a literal >

将它们放在一起,模式将从您提供的输入字符串中提取 email@email.com.

Putting it all together the pattern will extract email@email.com from the input string you have given it.

这篇关于正则表达式中的大于和小于符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆