与当前行的子字符串相关联的末端

发布于 2025-01-30 05:39:46 字数 893 浏览 3 评论 0原文

我已经搜寻了类似的东西,似乎无法提出解决方案。我有一些看起来像这样的文本:

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 技术交流群。

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

发布评论

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

评论(2

玩心态 2025-02-06 05:39:46

\ 1 BackReference仅在您的模式中有一个捕获组时才能使用:

(?<=@)(.+?)(?=\r)

如果您不使用捕获组,则应使用$&&&&&&&amp;而不是<<代码> \ 1 作为整个比赛的反向注册。此外,需要逃脱替换字符串中的括号。因此,替换字符串应该是:

amp;", acct.
amp;\);

您可能还需要使用$而不是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:

(?<=@)(.+?)(?=\r)

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:

amp;", acct.
amp;\);

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.

日裸衫吸 2025-02-06 05:39:46

您也可以省略lookbehind,并匹配 @,然后使用\ k清除当前的匹配缓冲区。

该行的其余部分

然后,您可以使用。+ 匹配 该行的其余部分。

在替换中,使用$ 0使用完整匹配,

请参见a REGEX DEMO 对于比赛:

找到:

@\K.+

替换为:

$0", acct.$0\)


​是的,您还可以将模式写为一种:

@\K.+(?=\r)
@\K.+(?=\R)

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:

@\K.+

Replace with:

$0", acct.$0\)

enter image description here


If there must be a newline to the right, you might also write the pattern as one of:

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