我有一个我想要匹配的LaTeX文档.我需要一个符合以下条件的RegEx匹配:
\ # the backslash in the beginning
[a-zA-Z]+ #a word
(\{.+\})* # any amount of {something}
Run Code Online (Sandbox Code Playgroud)
然而,她是抓住了;
在最后一行中,它需要贪婪,并且需要{}内部匹配数量.
意思是如果我有字符串\test{something\somthing{9}}
它将匹配整体.它需要按顺序({}).因此它与以下内容不匹配:
\ LaTeX {}是\ TeX {}的文档准备系统
只是
\胶乳{}
和
\ TeX的{}
帮助任何人?也许有人有更好的匹配想法?我不应该使用正则表达式吗?
这可以通过递归来完成:
$input = "\LaTeX{} is a document preparation system for the \TeX{}
\latex{something\somthing{9}}";
preg_match_all('~(?<token>
\\\\ # the slash in the beginning
[a-zA-Z]+ #a word
(\{[^{}]*((?P>token)[^{}]*)?\}) # {something}
)~x', $input, $matches);
Run Code Online (Sandbox Code Playgroud)
这正确匹配\LaTeX{}, \TeX{}, 和\latex{something\somthing{9}}
| 归档时间: |
|
| 查看次数: |
682 次 |
| 最近记录: |