正则表达式将匹配行包含在井号内但不包含前导空格

发布于 2024-08-23 20:21:12 字数 1489 浏览 3 评论 0原文

我可以在文本编辑器 (Jedit) 的查找/替换功能中使用正则表达式来执行以下操作:

匹配文本文件中满足以下条件的任何行:

  1. 该行以右括号结尾
  2. 某处存在左括号在同一行上

如果匹配,我需要将行上的所有文本换行 - 但不包括行开头的任何空格 - 位于 # 符号内。

示例 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:

  1. The line ends with a closing parenthesis
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时间你老了 2024-08-30 20:21:12

正则表达式:

^([ \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 [^)]*.

玻璃人 2024-08-30 20:21:12

我这样做了:

regex = /^(\s*)(.*)\((.*)\)$/

并在火柴上打印了 $1#$2($3)#。

I did this:

regex = /^(\s*)(.*)\((.*)\)$/

and printed $1#$2($3)# on a match.

大姐,你呐 2024-08-30 20:21:12

更新

好的,jEdit 默认正则表达式标志中的 [^(]+ ,也吃了 \n (我没有看到任何要设置的选项) jEdit 搜索/替换 UI 中的多行标志),

因此,这是新的,已使用更新的文本进行确认

搜索:^(\s*)([^(\n]+\([^)\n]* \))\s*$
替换:$1#$2

--- 先前的答案 ---

Jedit,

搜索:^(\s*)([^(]+\([^)]+\))\ s*$
替换:$1#$2

--- 上一个答案 ---

Python,'^(\s*)([^(]+\([^)]+\))\ s*$'

>>> import re
>>>
>>> re.sub('^(\s*)([^(]+\([^)]+\))\s*
  • 假设 \s* 行中只有一个括号,
  • 则不需要,如果有尾随空格,
  • 您可能需要 re.MULTILINE<如果您想一次性处理多行,也可以使用 /code> 标志。
,'\\1#\\2','Total reimbursements (before end of Q1)') '#Total reimbursements (before end of Q1)' >>> >>> re.sub('^(\s*)([^(]+\([^)]+\))\s*
  • 假设 \s* 行中只有一个括号,
  • 则不需要,如果有尾随空格,
  • 您可能需要 re.MULTILINE<如果您想一次性处理多行,也可以使用 /code> 标志。
,'\\1#\\2',' Total reimbursements (before end of Q1)') ' #Total reimbursements (before end of Q1)'
  • 假设 \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*$'

>>> import re
>>>
>>> re.sub('^(\s*)([^(]+\([^)]+\))\s*
  • assuming there is only one bracket in the line
  • \s* in the end would not need, if there is trailing spaces
  • you would probably need re.MULTILINE flag too, if you want to process multiple lines in one shot.
,'\\1#\\2','Total reimbursements (before end of Q1)') '#Total reimbursements (before end of Q1)' >>> >>> re.sub('^(\s*)([^(]+\([^)]+\))\s*
  • assuming there is only one bracket in the line
  • \s* in the end would not need, if there is trailing spaces
  • you would probably need re.MULTILINE flag too, if you want to process multiple lines in one shot.
,'\\1#\\2',' Total reimbursements (before end of Q1)') ' #Total reimbursements (before end of Q1)'
  • assuming there is only one bracket in the line
  • \s* in the end would not need, if there is trailing spaces
  • you would probably need re.MULTILINE flag too, if you want to process multiple lines in one shot.
り繁华旳梦境 2024-08-30 20:21:12

尝试以下操作:

^\s*(?=((.*)(?<=\((.*)\))$))|(?<=\((.*)\))$

仅当前面有左括号时,它才会向前和向后查找以匹配行末尾带有右括号的行。

用散列替换将为您提供所需的输出,它也会去除行开头的空格,如果这是您想要的目标但似乎最有可能的话,则不会。

Try the following:

^\s*(?=((.*)(?<=\((.*)\))$))|(?<=\((.*)\))$

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文