查找所有 PHP 短标签
我需要找到所有短 PHP 标签。
它的正则表达式 <\?(?!php)
但我不能在 vim 中使用它。
如何将其“转换”为vim?
I need to find all short PHP tags.
The regex for it <\?(?!php)
but I can not use it in vim.
How to "convert" it to vim?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
对我来说这个效果很好:
For me this one worked fine:
在 vim 中查找短标签的最佳方法是查找所有出现
且后面没有
p
的情况:您的正则表达式在 vim 中失败的原因是 < code>/? 查找文字问号,而
\?
是量词; vim 中的/<\?
将尝试查找 0 或 1 个小于号。这与您在大多数正则表达式引擎中所期望的相反。如果您想匹配紧随其后的换行符的短标记,则不能使用
[^p]
,这需要有 something 来匹配不存在的内容t ap
。在这种情况下,您可以将“not p or end-of-line”与The best way to find short-tags in vim is to find all occurrences of
<?
not followed by ap
:The reason your regex is failing in vim is because
/?
finds literal question marks, while\?
is a quantifier;/<\?
in vim will attempt to find 0 or 1 less-than signs. This is backwards from what you might expect in most regular expression engines.If you want to match short tags that are immediately followed by a new line, you cannot use
[^p]
, which requires there to be something there to match which isn't ap
. In this case, you can match "not p or end-of-line" with查找
替换
解释
仅匹配短标签
<\?
,后跟空格、制表符或换行符(\t|\n|\s) 并将其存储在
$1
中。此处提供的所有解决方案都与该行不包含的内容相匹配,而不是我们知道它将保证包含的内容。这允许您的代码格式保持相同,仅在需要时添加文本
php
。我们只想匹配短标签,而不应该关心后面的内容(php/xml/= 等)。此查找/替换将匹配内联短标签和单独行上的短标签。
注意:如果您使用 Windows 换行符,
<\?(\t|\r\n|\s)
将匹配相同的换行符。Find
Replace
Explanation
Match only short tags
<\?
, followed by white-space, tab or newline(\t|\n|\s)
and store it in$1
All of the solutions provided here match what the line doesn't contain, instead of what we know it will be guaranteed to contain. This allows your code formatting to remain identical, only adding the text
php
where needed.We only ever want to match the short tag, and shouldn't care what comes after (php/xml/= etc). This find/replace will match both inline short tags and short tags on a separate line.
Note: If you're using Windows line-breaks,
<\?(\t|\r\n|\s)
will match the same.使用(最新版本的)
grep
:查找带有
短标记的所有文件:
请注意,这些文件与
短标签,因为从 5.4 起始终可用 .0 并且这两种方式都没有坏处。 (而如果
allow_short_tags = Off
,会造成伤害。)
Using (a recent version of)
grep
:To find all files with a
<?
short tag:Note that these do not match the
<?=
short tag, as that is always available since 5.4.0 and it does no harm either way. (Whereas<?
does harm ifallow_short_tags = Off
.)您所追求的正确的前瞻模式是
,它将匹配除
php
后面的标签之外的所有短标签。有关外观的更多详细信息前/后断言只需在 vim 中输入
:help \@
The correct look ahead pattern you are after is
<?\(php\)\@!
that will match all short tags except those followed byphp
For more details on look ahead/behind assertions just type
:help \@
in vim对 Fredrik Pihl 的答案稍作修改
上面的答案与隐藏在我正在查看的某些代码中的开始短标记后跟换行符不匹配,这个确实......
A slight modification to the answer by Fredrik Pihl
The above answer didn't match an opening short tag followed by a newline that was hidden in some code I was looking at, this one does...
要匹配
而不是
,只需在 vim 中使用它作为搜索词:
/
在 cmd- 中开始搜索模式To match
<?
and not<?php
just use this as a search term in vim:/
to start the search in cmd-mode我想通过查找和替换将 PhpStorm 中的所有
标签替换为
。
对我来说,这个
查找
替换
我正在使用
$1
在 php 标记后添加 \n,否则它将用替换所有字符串
I want to replace all
<?
tags to<?php
in PhpStorm by find and replace.For me works this
find
replace
I'm using
$1
to add \n after php tag, otherwise it replace all sting with<?
以下正则表达式将仅捕获
如果您还想捕获
则使用此替代:
The following regex will only catch
<?
If you also want to catch
<?=
then use this instead: