如何从代码中删除缩进?
让我们考虑这个代码示例...不要看代码,而要看缩进。
protected function _hashPassword( $password, $salt, $nuts = '' ) {
if ( $nuts === '' ) {
$nuts = Kohana::config( 'a11n' )->nuts;
}
$password =
sha1(
$password
. $salt
. $nuts
);
return $password;
}
它取自更大的源代码。正如您所看到的,它缩进了 2 个制表符。我想以某种方式在不使用打字的情况下删除缩进。不知何故。
如果我使用编辑器内置“替换”功能并删除这两个选项卡,例如...
我得到这样的东西(不是在所有情况下,但几乎)...
protected function _hashPassword( $password, $salt, $nuts = '' ) {
if ( $nuts === '' ) {
$nuts = Kohana::config( 'a11n' )->nuts;
}
$password =
sha1(
$password
. $salt
. $nuts
);
return $password;
}
这是因为一行上不止两个选项卡,它取代了所有 4 个选项卡。
我正在寻找足够强大的正则表达式来很好地删除缩进!也许还有其他解决方案吗?只是不建议编写没有缩进的代码!
Lets consider this example of code... Don't look at code, but at indents.
protected function _hashPassword( $password, $salt, $nuts = '' ) {
if ( $nuts === '' ) {
$nuts = Kohana::config( 'a11n' )->nuts;
}
$password =
sha1(
$password
. $salt
. $nuts
);
return $password;
}
It's taken from much bigger source code. As you can see, it's indented by 2 tabs. I want to somehow remove indent from it without using typing. Somehow.
If I use in-editor build-in 'Replace' function and remove those two tabs like...
I get something like this (not in all cases, but almost)...
protected function _hashPassword( $password, $salt, $nuts = '' ) {
if ( $nuts === '' ) {
$nuts = Kohana::config( 'a11n' )->nuts;
}
$password =
sha1(
$password
. $salt
. $nuts
);
return $password;
}
It's because there are more than only two tabs on one line and it replaces all 4 tabs.
I'm looking for regular expression that's powerful enough to remove indent nicely! Maybe there are any other solutions? Just don't suggest to write code without indents!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您最喜欢的现代编辑器中选择代码,然后按 Shift+Tab。
Select the code in your favorite modern editor, and hit Shift+Tab.
当然,您想简单地替换
^\t\t
,或者使用编辑器具有的任何字符作为行首锚点,如果它不是^
(我不知道您的特定编辑器的功能是什么,只是(相当好)了解大多数正则表达式引擎的驱动方式)。这只会替换每行开头的两个选项卡项目。
例如,如果我在 Linux 下从命令行执行此操作,
就会执行您想要的操作。
Surely you want to simply replace
^\t\t
, or use whatever character your editor has for the start-of-line anchor if it's not^
(I have no idea what the capabilities of your particular editor are, only a (pretty good) understanding of how most regular expression engines are driven).This will only replace two-tab items at the start of each line.
For example, if I were doing this from the command line under Linux,
would do what you desire.
使用正则表达式,将
^\t\t
替换为空字符串。在不使用正则表达式的情况下,只需将
\n\t\t
替换为\n
即可。另请考虑使用源代码格式化程序。
Using regular expressions, replace
^\t\t
with the empty string.Without using regular expressions, simply replace
\n\t\t
with\n
.Also consider using a source code formatter.
以下内容适用于 Notepad++:
1) 将搜索模式设置为:正则表达式
2) 查找内容:
^[\t]+
3) 替换为:
编辑:这将从头开始替换 1 个或多个选项卡每行。
替代方法:选择整个源代码并多次点击
Shift-Tab
。它将删除每行开头的制表符,就像上面的正则表达式一样。我希望这有帮助!The following applies to Notepad++:
1) Set search mode to: Regular expression
2) Find what:
^[\t]+
3) Replace with:
Edit: This will replace 1 or more tabs from the beginning of every line.
Alternative method: select the whole source code and hit
Shift-Tab
multiple times. It will remove the tabs from the beginning of every line, just like the regex above. I hope this helps!