Difference between revisions of "Help:Regular expressions"

From Nookipedia, the Animal Crossing wiki
(Creating)
 
(→‎Examples: Add example from AWB for adding images to house templates)
(4 intermediate revisions by 2 users not shown)
Line 13: Line 13:
 
* [https://regex101.com/ regex101.com]
 
* [https://regex101.com/ regex101.com]
 
* [https://regexr.com/ regexr.com]
 
* [https://regexr.com/ regexr.com]
 +
* [https://extendsclass.com/regex-tester.html extendsclass.com]
  
 
== Examples ==
 
== Examples ==
 
Below is a list of examples of regular expressions that may help you on the wiki. Feel free to expand this list.
 
Below is a list of examples of regular expressions that may help you on the wiki. Feel free to expand this list.
 +
 +
* Replacing instances of <code><nowiki>{{I|Item Name}}</nowiki></code> with <code><nowiki>{{I|Item Name|New Horizons}}</nowiki></code> (Python):
 +
:Find: <code><nowiki>{{I\|(?!.*\|)(.*?)}}</nowiki></code>
 +
:Replace: <code><nowiki>{{I|\1|New Horizons}}</nowiki></code>
  
 
* Matching a multi-line Foreignname template and adding a new "See also" section after it (Python):
 
* Matching a multi-line Foreignname template and adding a new "See also" section after it (Python):
 
:Find: <code><nowiki>{{Foreignname([\s\S]*?\n}})</nowiki></code>
 
:Find: <code><nowiki>{{Foreignname([\s\S]*?\n}})</nowiki></code>
:Replace: <code><nowiki>{Foreignname\1\n\n==See also==</nowiki></code>
+
:Replace: <code><nowiki>{{Foreignname\1\n\n==See also==</nowiki></code>
 +
 
 +
* Add <code>f#-img</code> parameters after each <code>furn#</code> parameter in instances of house templates; the template takes in the furniture name as a parameter and returns a filename (AWB):
 +
:Find: <code><nowiki>\|furn([1-9]\d?)= (.*)</nowiki></code>
 +
:Replace: <code><nowiki>|furn$1= $2\n|f$1-img= {{subst:Example|$2}}</nowiki></code>
 +
 
 +
[[Category:Nookipedia]]

Revision as of 04:02, March 3, 2021

A regular expression (regex) is a sequence of characters defining a search pattern. On the wiki, they are commonly used for finding and replacing text through Special:ReplaceText (staff only), AutoWikiBrowser, Pywikibot, or Scribunto/Lua modules.

Each tool/programming language uses its own ruleset for regular expressions:

If you need help with regular expressions, you may post on the talk page, or join the Nookipedia Discord and ask in the #wiki-tech channel.

Testing

There are several online tools to test our patterns. A small selection is listed below.

Examples

Below is a list of examples of regular expressions that may help you on the wiki. Feel free to expand this list.

  • Replacing instances of {{I|Item Name}} with {{I|Item Name|New Horizons}} (Python):
Find: {{I\|(?!.*\|)(.*?)}}
Replace: {{I|\1|New Horizons}}
  • Matching a multi-line Foreignname template and adding a new "See also" section after it (Python):
Find: {{Foreignname([\s\S]*?\n}})
Replace: {{Foreignname\1\n\n==See also==
  • Add f#-img parameters after each furn# parameter in instances of house templates; the template takes in the furniture name as a parameter and returns a filename (AWB):
Find: \|furn([1-9]\d?)= (.*)
Replace: |furn$1= $2\n|f$1-img= {{subst:Example|$2}}