使用正则表达式在Notepad++中查找所有条目,只要它们具有不同的数字。

huangapple go评论41阅读模式
英文:

Find all entries while they have different numbers in notepad++ using regex

问题

我有一些可能包含以下内容的文件:

npc[0].type=XY
npc[1].type=XY
npc[2].type=XY

等等。

方括号 [] 中的数字可以是从 0 到 9 中的某个数字。

我需要在文件中找到所有包含这种行的情况。

我目前尝试了 npc[\d{1}].type= ,但是找不到任何结果。

我期望找到所有这些行的情况,无论方括号内的数字是多少。

英文:

I have some files that may contain these lines:

npc[0].type=XY
npc[1].type=XY
npc[2].type=XY 

and so on.

The number in the brackets [] can be something from 0 to 9.

I need to find all cases in the files where such a line exists.

I tried npc[\d{1}].type= so far, but then it finds 0 results.

I expect to find all cases of these lines, regardless of the number within the brackets

答案1

得分: 1

^npc\[\d{1}].+$ 的翻译如下:

^ = 字符串的开头

npc\[ = 文本 "npc["

\d{1} = 精确一个数字

\] = 文本 "]"

.+ = 除换行符外的任何字符,至少1次或更多次出现

$ = 字符串的结尾

您可能需要添加全局和不区分大小写的标志(gi)。

英文:
^npc\[\d{1}\].+$

Explained:

^ = Start of String

npc\[ = The text "npc["

\d{1} = Exactly one digit

\] = The text "]"

.+ = Any character except newline with at least 1 or more occurrences

$ = End of String

You may want to add global & case-insensitive flags (gi).

答案2

得分: 0

逃避起始方括号 \[ 并逃避点以匹配其字面值 \. 并省略 {1},因为它是默认值:

npc\[\d]\.type=

正则表达式演示

英文:

Escape the opening square bracket \[ and escape the dot to match it literally \. and omit {1} as that is the default:

npc\[\d]\.type=

Regex demo

答案3

得分: 0

维基百科对正则表达式模式语法有很好的文档。维基百科 – 正则表达式 – 语法

你的错误在于方括号字符是元字符,它们被解释为语法。

在元字符前加上反斜杠字符 \,以将其“取消转义”为语法。

npc\[\d{1}\].type= 

另外,数量词 1 会显得多余,并且最终是可选的,如果是 1。

英文:

Wikipedia has great documentation on regular expression pattern syntax.
Wikipedia – Regular expression – Syntax.

Your error is that the square-bracket characters are meta-characters—they're being interpreted as syntax.

Precede a meta-character with a reverse-solidus character, \, to "un-escape" it as syntax.

npc\[\d{1}\].type= 

Additionally, the quantifier of 1 would be redundant—and untimately, is optional if 1.

huangapple
  • 本文由 发表于 2023年6月5日 23:50:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定