[英]How do I regex search in x and y for a, and only include the replacement of y if a was found in x?
I need to search through a larger text file. 我需要搜索更大的文本文件。
This is an example of what I'm searching through. 这是我正在搜索的示例。
https://pastebin.com/JFVy2TEt https://pastebin.com/JFVy2TEt
recipes.addShaped("basemetals:adamantine_arrow", <basemetals:adamantine_arrow> * 4, [[<ore:nuggetAdamantine>], [<basemetals:adamantine_rod>], [<minecraft:feather>]]);
I need to look for lines that match a specific part in the first argument. 我需要在第一个参数中查找与特定部分匹配的行。
For example the "_arrow" part in the above line. 例如,上一行中的“ _arrow”部分。
And erase everything that doesn't match on the "_arrow" in the first argument. 并删除第一个参数中与“ _arrow”不匹配的所有内容。
And the arguments differ across all of them. 所有这些人的论点都不尽相同。
And also with different names in the place where "basemetals:adamantine" is in the above line. 上面一行中“贱金属:金刚烷”的位置也有不同的名称。
And since the further arguments are all different I can't wrap my head around on how to include the end only when the first thing matches. 而且,由于进一步的论点都是不同的,所以只有在第一件事匹配时,我才能继续思考如何包括结尾。
Edit: The end goal being to ease sort my 3k+ line text file. 编辑:最终目标是简化我的3k +行文本文件的排序。
basic, blacksmith, carpenter, chef, chemist, engineer, farmer, jeweler, mage, mason, scribe, tailor 基本,铁匠,木匠,厨师,化学家,工程师,农夫,珠宝商,法师,梅森,抄写员,裁缝
I think what you're trying to do is filter your text file by removing lines that don't fit a set criteria. 我认为您要尝试的是通过删除不符合设置条件的行来过滤文本文件。 I've chosen the Atom text editor for this solution (because I'm running Windows OS and can't install gedit, and I want to ensure you have a working example).
我为此解决方案选择了Atom文本编辑器(因为我正在运行Windows操作系统,并且无法安装gedit,因此我想确保您有一个有效的示例)。
To remove only lines that don't have a first argument ending in _arrow
, one could do (?!recipes\\.addShaped\\("[^"]+_arrow")recipes.+\\r?\\n?
and replace with nothing. 要只删除没有以
_arrow
结尾的第一个参数的_arrow
,可以执行(?!recipes\\.addShaped\\("[^"]+_arrow")recipes.+\\r?\\n?
并且不进行任何替换。
As a note: this task is made more difficult by Atom's low regex support. 注意:Atom的低正则表达式支持使此任务变得更加困难。 In a more well-supported environment, my answer would probably be
^recipes\\.addShaped("[^"]+(?<!_arrow)").+\\r?\\n?
(with multiline mode). 在更受支持的环境中,我的答案可能是
^recipes\\.addShaped("[^"]+(?<!_arrow)").+\\r?\\n?
(使用多行模式)。
Also, please read "What should I do when someone answers my question?" 另外,请阅读“有人回答我的问题该怎么办?” .
。
Regex explained: 正则表达式解释:
(?!
)
is a negative lookahead , which peeks at the succeeding text to ensure it doesn't contain " _arrow
" at end of the first argument. (?!
)
是一个否定的超前行为 ,它会窥视后续文本以确保其在第一个参数的末尾不包含“ _arrow
”。 \\.
is an escaped literal period [^"]
is a character class that signifies a character that is not a "
. [^"]
是一个字符类,表示不是 "
的字符。 +
is a quantifier which tells the regex to match the preceding character or subexpression as many times as possible, with a minimum of one time. +
是一个量词,它告诉正则表达式尽可能多地匹配前面的字符或子表达式,至少要匹配一次。 .
is a wildcard, representing any character \\r?\\n?
is used to match any kind of newline, with the ?
?
quantifier making each character optional.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.