与当前行的子字符串相关联的末端
我已经搜寻了类似的东西,似乎无法提出解决方案。我有一些看起来像这样的文本:
command.Parameters.Add("@Id
command.Parameters.Add("@IsDeleted
command.Parameters.Add("@MasterRecordId
command.Parameters.Add("@Name
...
我希望文字最终会这样:
command.Parameters.Add("@Id", acct.Id);
command.Parameters.Add("@IsDeleted", acct.IsDeleted);
command.Parameters.Add("@MasterRecordId", acct.MasterRecordId);
command.Parameters.Add("@Name", acct.Name);
...
如您所见,我本质上想将界限的结尾附加到:“,acct。<第二个“>);
我正在尝试:
找到什么:(?< =@)。+?(?= \ r)
- 这有效,它找到了适当的单词。
替换:\ 1“,acct。\ 1);
- 这不是。它将行更改为(for ID):
command.Parameters.Add("@", acct.
不确定我在做什么错。我认为> \ 1
应该是“查找什么”框中的“捕获”,但我猜不是吗?
I've scoured Stack Overflow for something just like this and can't seem to come up with a solution. I've got some text that looks like this:
command.Parameters.Add("@Id
command.Parameters.Add("@IsDeleted
command.Parameters.Add("@MasterRecordId
command.Parameters.Add("@Name
...
And I would like the text to end up like this:
command.Parameters.Add("@Id", acct.Id);
command.Parameters.Add("@IsDeleted", acct.IsDeleted);
command.Parameters.Add("@MasterRecordId", acct.MasterRecordId);
command.Parameters.Add("@Name", acct.Name);
...
As you can see, I essentially want to append the end of the line with: ", acct.<word between @ and second ">);
I'm trying this:
Find What: (?<=@).+?(?=\r)
- This works, it finds the appropriate word.
Replace: \1", acct.\1);
- This doesn't. It changes the line to (for Id):
command.Parameters.Add("@", acct.
Not sure what I'm doing wrong. I thought that \1
is supposed to be the "capture" from the "Find what" box, but it's not I guess?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
\ 1
BackReference仅在您的模式中有一个捕获组时才能使用:如果您不使用捕获组,则应使用
$&&&&&&&amp;
而不是<<代码> \ 1 作为整个比赛的反向注册。此外,需要逃脱替换字符串中的括号。因此,替换字符串应该是:您可能还需要使用
$
而不是lookahead(?= \ r)
,以防最后一行不紧随其后EOL角色。说了这么多,我个人更喜欢在做正则替代时更加明确/严格,以免弄乱其他线(即误报)。因此,我会选择这样的事情:
查找:
(\ bcommand \ .parameters \ .add \(“@)(\ w+)(\ w+)$
替换:
\ 1 \ 1 \ 2”,acct。 \ 2 \);
请注意,
\ w
只能匹配单词字符,这可能是这里所需的行为。如果您认为标识符可能具有其他字符,请随时用角色类替换。The
\1
backreference will only work if you have a capturing group in your pattern:If you're not using a capturing group, you should use
$&
instead of\1
as a backreference for the entire match. Additionally, parentheses in the replacement string need to be escaped. So, the replacement string should be:You might also want to use
$
instead of the Lookahead(?=\r)
in case the last line isn't followed by an EOL character.Having said all that, I personally prefer to be more explicit/strict when doing regex substitution to avoid messing up other lines (i.e., false positives). So I would go with something like this:
Find:
(\bcommand\.Parameters\.Add\("@)(\w+)$
Replace:
\1\2", acct.\2\);
Note that
\w
will only match word characters, which is likely the desired behavior here. Feel free to replace it with a character class if you think your identifiers might have other characters.您也可以省略lookbehind,并匹配 @,然后使用
\ k
清除当前的匹配缓冲区。该行的其余部分
然后,您可以使用
。+
匹配 该行的其余部分。在替换中,使用
$ 0
使用完整匹配,请参见a REGEX DEMO 对于比赛:
找到:
替换为:
是的,您还可以将模式写为一种:
You could also omit the lookbehind, and match the @ and then use
\K
to clear the current match buffer.Then you can match the rest of the line using
.+
Note that you don't have to make the quantifier non greedy
.*?
as you are matching the rest of the line.In the replacement, use the full match using
$0
See a regex demo for the matches:
Find what:
Replace with:
If there must be a newline to the right, you might also write the pattern as one of: