简体   繁体   English

正则表达式可查找特定的字符串并在记事本++中尚不存在时添加字符

[英]Regular expression to find specific string and add characters when the're not already there in notepad++

Okay, I have zero knowledge of regular expressions so if someone can direct me to a better way to figure this out then by all means please do. 好的,我对正则表达式的知识为零,所以如果有人可以引导我找到一种更好的方法来解决这个问题,那么请务必这样做。

I figured out that a series of files are missing a particular naming convention for the database they will write to. 我发现一系列文件缺少它们要写入的数据库的特定命名约定。 So some might be dbname1, dbname2, dbname3, abcdbname4, abcdbname5 and they all need to have that abc in the beginning. 因此,其中一些可能是dbname1,dbname2,dbname3,abcdbname4,abcdbname5,并且它们都需要在开始时包含该abc。 I want to write a regular expression that will find all tags in the file that do not follow immediately by abc and add in abc. 我想编写一个正则表达式,以查找文件中所有不立即由abc跟随的标签,并添加abc。 Any ideas how I can do this? 有什么想法可以做到吗?

Again, forgive me if this is poorly worded/expressed. 再次,请原谅我措辞/表达不好。 I really have absolutely zero knowledge of regular expressions. 我真的对正则表达式一无所知。 I can't find any questions that are asking this. 我找不到任何问这个的问题。 I know that there are questions asking how to add strings to lines but not how to add only to lines that are missing the string when some already have it. 我知道有些问题问如何在行中添加字符串,而不是仅在缺少字符串的情况下仅添加到某些行中。

I thought I had written this in but I'm looking at lines that look like this 我以为是我写的,但我正在看的像这样的线条

<Name>dbname</Name> 

or 要么

<Name>abcdbname</Name> 

and I need to get them all to have that abc at the beginning 我需要让他们所有人在开始时都有那个abc

Replace \\bdbname(\\d+) with abcdbname\\1 . \\bdbname(\\d+)替换为abcdbname\\1

The \\b means "word boundary", so it won't match the abc versions, but will match the others. \\b表示“单词边界”,因此它与abc版本不匹配,但与其他版本匹配。 The (...) parentheses represent a capturing group, which capture everything that's matched in-between into a numbered variable that can be later referenced (there's only one here so it goes in \\1 ). (...)括号表示捕获组,捕获组之间的所有匹配项都被捕获到一个编号的变量中,以后可以引用该编号的变量(这里只有一个,因此它位于\\1 )。 The \\d+ matches one or more digit characters. \\d+匹配一个或多个数字字符。

Cameron's answer will work, but so will this. 卡梅伦的答案会奏效,但这也会奏效。 It's called a negative lookbehind. 称为负向后看。

(?<!abc)(dbname\d+)

This regex looks for dbname followed by 1 or more digits, and not prefixed by abc. 此正则表达式查找dbname,后跟1个或多个数字,并且不带abc前缀。 So it will capture dbname113. 因此它将捕获dbname113。

This looks for any occurrence of dbname not immediately prefixed by the string "abc". 这将查找没有立即用字符串“ abc”作为前缀的dbname的任何出现。 THe original name is in the capture group \\1 so you can replace this regex with abc\\1 and all your files will be properly prefixed. 原始名称位于捕获组\\ 1中,因此您可以将该正则表达式替换为abc\\1并且所有文件都将正确加上前缀。

Not every program/language that implements regex (famously, javascript) supports lookbehinds, but most do and Notepad++ certainly does. 并非每个实现正则表达式的程序/语言(众所周知,都是javascript)都支持lookbehinds,但是大多数都支持,而Notepad ++确实支持。 Lookarounds (lookbehind / lookaheads) are exceedingly handy once you get the hang of them. 一旦掌握了环顾处(环顾四周/向前看),便非常方便。

?<! negative lookbehind, ?<= positive lookbehind / lookbehind, ?! 负向后看, ?<=正向后看/向后看, ?! negative lookhead, and ?= lookahead all must be used within parantheses as I did above, but they're not used in capturing so they do not create capture groups, hence why the second set of parentheses is able to be referenced as \\1 (or $1 depending on the language) 否定的lookhead和?= lookahead都必须在括号中使用,就像我在上面所做的那样,但是它们没有用于捕获,因此它们不会创建捕获组,因此为什么第二组括号可以引用为\\ 1(或$ 1,视语言而定)

Edit: Given some better example criteria, this is possibly more what you're looking for. 编辑:给定一些更好的示例标准,这可能是您正在寻找的更多内容。

Find: (<Name>)(.*?(?<!abc)dbname\d+)(</Name>)
Replace: \1abc\2\3

Alternatively, something a bit easier to understand, you can do this or something like this: 另外,您也可以执行以下操作:

Find: (<Name>)(abc)?(dbname\d+)(</Name>)
Replace: \1abc\3\4

What this is does is: 这是做什么的:

  • Matches <Name> , captures as backreference 1. 匹配<Name> ,捕获为反向引用1。
  • Looks for abc and captures it, if it's there as backreference 2, otherwise 2 contains nothing. 查找abc并将其捕获(如果它作为后向引用2存在),否则2不包含任何内容。 The ? ? after (abc) means match 0 or 1 times. 之后(abc)表示匹配0或1次。
  • Looks for the dbname and captures it. 查找dbname并捕获它。 and captures as backreference 3. 并捕获为反向引用3。
  • Matches </Name> , captures as backreference 4. 匹配</Name> ,捕获为反向引用4。

By replacing with \\1abc\\3\\4, you kind of drop abc off dbname if it exists and replace dbname with abcdbname in all instances. 通过用\\ 1abc \\ 3 \\ 4替换,您可以将abcname从dbname中删除(如果存在),然后在所有实例中都将dbname替换为abcdbname。

You can take this a step further and 您可以更进一步,

Find: (<Name>)(?:abc)?(dbname\d+)(</Name>)
Replace: \1abc\2\3

prefix the abc with ?: to create a noncapturing group, so the backreferences for replacing are sequential. abc前面加上?:前缀以创建一个非捕获组,因此用于替换的反向引用是顺序的。

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

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