简体   繁体   中英

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

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.

And erase everything that doesn't match on the "_arrow" in the first argument.

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.

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).

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.

As a note: this task is made more difficult by Atom's low regex support. In a more well-supported environment, my answer would probably be ^recipes\\.addShaped("[^"]+(?<!_arrow)").+\\r?\\n? (with multiline mode).

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.
  • \\. 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.
  • Everything else it literal characters; it represents exactly what it matches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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