简体   繁体   English

awk、sed 或 vim 正则表达式

[英]awk, sed or vim regex

I'm searching for the best way to add a string to an existing string while I don't want to replace the whole string.我正在寻找将字符串添加到现有字符串的最佳方法,而我不想替换整个字符串。

self.fields_desc.append(BitField("foo", 0x3, 4))

sould be replaced by:应替换为:

self.fields_desc.append(BitField("foo" + str(self.__class__.i), 0x3, 4))

Using which tool would allow me to do this with the less possible trouble?使用哪种工具可以让我以更少的麻烦做到这一点? In vim I could do:在 vim 我可以这样做:

:%s/self.fields_desc.append(BitField("[a-zA-Z0-9]*", 0x[0-9]*, [0-9]*))/self.fields_desc.append(BitField("foo" + str(self.__class__.i), 0x3, 4))/g

But I don't know how I would tell vim to not replace the regex I wrote.但我不知道如何告诉 vim 不要替换我写的正则表达式。 Could you give me a hand on this please?你能帮我解决这个问题吗?

Use capturing groups (note the "\" before the "(" and ")", and the "\1", "\2" etc):使用捕获组(注意“(”和“)”之前的“\”,以及“\1”、“\2”等):

:%s/self\.fields_desc\.append(BitField(\("[a-zA-Z0-9]*"\), \(0x[0-9]\+\), \([0-9]\+\)))/self.fields_desc.append(BitField(\1 + str(self.__class__.i), \2, \3))/g

changes:变化:

self.fields_desc.append(BitField("foo", 0x3, 4))
self.fields_desc.append(BitField("test", 0x5, 3))

to

self.fields_desc.append(BitField("foo" + str(self.__class__.i), 0x3, 4))
self.fields_desc.append(BitField("test" + str(self.__class__.i), 0x5, 3))

Note:笔记:

  1. I've escaped the ".", as "."我已经将“.”转义为“。” matches any character (except newline), and you want a literal "."匹配任何字符(换行符除外),并且您需要文字“。” character.特点。
  2. I've replaced * with + for the number matches: I doubt you want to match self.fields_desc.append(BitField("foo", 0x,) etc.我已经用+替换了*来匹配数字:我怀疑你想匹配self.fields_desc.append(BitField("foo", 0x,)等。
  3. If you aren't sure whether the spacing is correct, ie, you don't always have self.fields_desc.append(BitField("foo", 0x3... but sometimes self.fields_desc.append(BitField("foo",0x3 or self.fields_desc.append(BitField("foo", 0x3 , then add a * after the space characters. Although I'd suggest insteading standardising your code.如果您不确定间距是否正确,即您并不总是有self.fields_desc.append(BitField("foo", 0x3...但有时self.fields_desc.append(BitField("foo",0x3self.fields_desc.append(BitField("foo", 0x3 ,然后在空格字符后添加* 。尽管我建议您不要标准化您的代码。

See Regex grouping and The regex "dot" .请参阅正则表达式分组正则表达式“点”


As sidyll says, it is probably better to learn to use the built-in character classes "\d", "\w" (see Shorthand character classes ) and so on:正如 sidyll 所说,学习使用内置字符类“\d”、“\w”(参见速记字符类)等可能会更好:

:%s/self\.fields_desc\.append(BitField(\("\w*"\), \(0x\d\+\), \(\d\+\)))/self.fields_desc.append(BitField(\1 + str(self.__class__.i), \2, \3))/g

This is both for brevity, and readability.这是为了简洁和可读性。 Also, otherwise, readers will assume you have some special reason for defining your own character class (ie, they will read it twice to make sure there's not some unknown character in there).此外,否则,读者会假设您有一些特殊原因来定义自己的字符 class(即,他们会阅读两次以确保其中没有未知字符)。

Please please avoid unnecessary matches and replaces.请避免不必要的匹配和替换。 Use built-in character classes.使用内置字符类。 And escape those dots.并避开那些点。 There is no need to group things here.这里没有必要对事物进行分组。

:%s/self\.fields_desc\.append(BitField("\w*"\zs\ze, 0x\d, \d))/ + str(self.__class__.i)

You probably don't need the g flag, I doubt two of these will appear on the same line.您可能不需要g标志,我怀疑其中两个会出现在同一行。

See :h \zs and :h \ze .请参阅:h \zs:h \ze

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM