Difference between revisions of "Help:Regular expressions"
From Nookipedia, the Animal Crossing wiki
SuperHamster (talk | contribs) (→Examples: Add example from AWB for adding images to house templates) |
SuperHamster (talk | contribs) (→Examples: Removing deprecated HTML example, since it's subject to errors if multiple tags are on one line) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 30: | Line 30: | ||
:Replace: <code><nowiki>|furn$1= $2\n|f$1-img= {{subst:Example|$2}}</nowiki></code> | :Replace: <code><nowiki>|furn$1= $2\n|f$1-img= {{subst:Example|$2}}</nowiki></code> | ||
− | [[ | + | * Prepend an <code>html</code> selector to every CSS property in a stylesheet ([https://stackoverflow.com/a/37695196 source] on StackOverflow by [https://stackoverflow.com/users/6438812/ryan-worth Ryan Worth]): |
+ | :Find: <code><nowiki>(^(?:\s|[^@{])*?|[},]\s*)(\/\/.*\s+|.*\/\*[^*]*\*\/\s*|@media.*{\s*|@font-face.*{\s*)*([.#]?-?[_a-zA-Z]+[_a-zA-Z0-9-]*)(?=[^}]*{)</nowiki></code> | ||
+ | :Replace: <code><nowiki>$1$2html $3</nowiki></code> | ||
+ | |||
+ | {{Nookipedia}} |
Revision as of 03:58, July 7, 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:
- Special:ReplaceText supports a small subset (
( ) . * + ? [ ]
) of the PHP and MySQL/PostgreSQL regular-expression set. - AutoWikiBrowser uses .NET regular expressions.
- Pywikibot uses Python regular expressions.
- Lua/Scribunto pattern are similar to regular expressions, but not identical. See this page for a list of differences.
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 eachfurn#
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}}
- Prepend an
html
selector to every CSS property in a stylesheet (source on StackOverflow by Ryan Worth):
- Find:
(^(?:\s|[^@{])*?|[},]\s*)(\/\/.*\s+|.*\/\*[^*]*\*\/\s*|@media.*{\s*|@font-face.*{\s*)*([.#]?-?[_a-zA-Z]+[_a-zA-Z0-9-]*)(?=[^}]*{)
- Replace:
$1$2html $3