正则表达式将匹配行包含在井号内但不包含前导空格
我可以在文本编辑器 (Jedit) 的查找/替换功能中使用正则表达式来执行以下操作:
匹配文本文件中满足以下条件的任何行:
- 该行以右括号结尾
- 某处存在左括号在同一行上
如果匹配,我需要将行上的所有文本换行 - 但不包括行开头的任何空格 - 位于 #
符号内。
示例 1
此行:
Total reimbursements (before end of Q1)
需要替换为:
#Total reimbursements (before end of Q1)#
示例 2(前导空格)
此行(其中Total): 一词之前有空格:
Total reimbursements (before end of Q1)
需要用此替换(# 符号放置在该行的第一个字母之前):
#Total reimbursements (before end of Q1)#
但不能用此替换:
# Total reimbursements (before end of Q1)#
示例文本文件:
Base Expenses
&&&&&&&&&&&&&&&&&&&&&&&
Provides options towards multilateral improvements
Opening Debt(Option patterns)
A copy provided externally
Customer Summary
&&&&&&&&&&&&&&&&&&&&&&&&&
External Console(foreign debt)
Provide execution amounts
Internal Console(domestic debt)
Release to appropriations committee
Explanations provided to external clients
Neutralized Amounts()
Forex portion
Is there a Regular Expression I could use in the Find/Replace feature of my text editor (Jedit) to do the following:
Match any lines in a text file that meet these criteria:
- The line ends with a closing parenthesis
- An opening parenthesis exists somewhere on the same line
If it matches I need to wrap all of the text on the line - but not any whitespace at the start of the line- inside #
signs.
Example 1
This line:
Total reimbursements (before end of Q1)
needs to be replaced with this:
#Total reimbursements (before end of Q1)#
Example 2 (leading whitespace)
This line (where there is whitespace before the word Total):
Total reimbursements (before end of Q1)
needs to be replaced with this (the # sign is placed before the first letter on the line):
#Total reimbursements (before end of Q1)#
but NOT with this:
# Total reimbursements (before end of Q1)#
Sample text file:
Base Expenses
&&&&&&&&&&&&&&&&&&&&&&&
Provides options towards multilateral improvements
Opening Debt(Option patterns)
A copy provided externally
Customer Summary
&&&&&&&&&&&&&&&&&&&&&&&&&
External Console(foreign debt)
Provide execution amounts
Internal Console(domestic debt)
Release to appropriations committee
Explanations provided to external clients
Neutralized Amounts()
Forex portion
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正则表达式:
^([ \t]*)(.*\(.*\))$
替换:
$1#$2#
最棘手的事情是确保没有正则表达式可以匹配换行符。这就是为什么我使用
[ \t]*
而不是\s*
和.*
而不是[^(]* 或 <代码>[^)]*。
Regex:
^([ \t]*)(.*\(.*\))$
Replacement:
$1#$2#
The trickiest thing is making sure no part of the regex can match newlines. That's why I used
[ \t]*
instead of\s*
and.*
instead of[^(]*
or[^)]*
.我这样做了:
并在火柴上打印了 $1#$2($3)#。
I did this:
and printed $1#$2($3)# on a match.
更新:
好的,jEdit 默认正则表达式标志中的
[^(]+
,也吃了\n
(我没有看到任何要设置的选项) jEdit 搜索/替换 UI 中的多行标志),因此,这是新的,已使用更新的文本进行确认
搜索:
^(\s*)([^(\n]+\([^)\n]* \))\s*$
替换:
$1#$2
--- 先前的答案 ---
Jedit,
搜索:
^(\s*)([^(]+\([^)]+\))\ s*$
替换:
$1#$2
--- 上一个答案 ---
Python,
'^(\s*)([^(]+\([^)]+\))\ s*$'
\s*
行中只有一个括号,re.MULTILINE<如果您想一次性处理多行,也可以使用 /code> 标志。
UPDATE:
Ok,
[^(]+
in jEdit default regex flag, eaten\n
too (I don't see any options to set multiline flag in jEdit search/replace UI),So, here is new one, confirmed with your updated text
Search:
^(\s*)([^(\n]+\([^)\n]*\))\s*$
Replace:
$1#$2
--- previous answer ---
Jedit,
Search :
^(\s*)([^(]+\([^)]+\))\s*$
Replace :
$1#$2
--- previous previous Answer ---
Python,
'^(\s*)([^(]+\([^)]+\))\s*$'
\s*
in the end would not need, if there is trailing spacesre.MULTILINE
flag too, if you want to process multiple lines in one shot.尝试以下操作:
仅当前面有左括号时,它才会向前和向后查找以匹配行末尾带有右括号的行。
用散列替换将为您提供所需的输出,它也会去除行开头的空格,如果这是您想要的目标但似乎最有可能的话,则不会。
Try the following:
It looks ahead and behind to match lines with a closing bracket at the end of the line only if preceded by an opening bracket.
Replacing with a hash will give you the desired output, it will strip the whitespace at the start of the line also, not suer if this is your desired goal but seems most likely.