Extension talk:Replace Text

Cojoilustrado (talkcontribs)

Trying to replace |names=Mark|surnames=Smith for |name={{name|Mark|Smith}} but I have failed so far. The regex I used is nombres\=(.*)|apellidos\=(.*) to be replaced by nombre={{nombre|$1|$2}}, but this is obviously wrong because I get back nonsense. Anyone can help me to figure this one out? Thank you.

Dinoguy1000 (talkcontribs)

You don't need to escape the equals signs, but you do need to escape the pipe: nombres=(.*)\|apellidos=(.*)

Though this regex is also unlikely to do what you want. Unfortunately, there is no fast or easy answer for this as long as you're using regex for the replacement; you're going to have to spend some time familiarizing yourself with basic regex functionality, and depending on how the parameters you're trying to replace are used, it may not even be possible with just regex.

Cojoilustrado (talkcontribs)

I'll give it run and see where I get. Do you know of any site with a regex guide I can learn from? The one from Mysql in the usage section might as well be in Mandarin to me XD. Thank you so much. It's a great and useful extension. It just has a leanring curve.

Dinoguy1000 (talkcontribs)

My go-to reference is generally regular-expressions.info, though I wouldn't be surprised if others can chime in with superior beginner-oriented materials. I know there are also sites that allow you to test regexes (including fairly advanced offerings that can syntax-highlight regexes and explain each individual component, and even walk through the matching process on sample/provided texts), but I personally don't often use these so don't have any recommendations.

Cojoilustrado (talkcontribs)

Thank you!!

Reply to "How do you..."
Cojoilustrado (talkcontribs)

On the results page, every word is capitalized. This makes it very difficult to copy and paste strings that might be of interest in another text replacement. For example, if I'm looking for this misspelling, "carwach", I might get this text, "Carwach Is A Commedy Filmm". If I now want to look for "Commedy Filmm", the search might have no hits because the words in the source might be "Commedy filmm" or "commedy Filmm" or "commedy filmm" but not "Commedy Filmm" (all caps). I think it would be an improvement to the extension to display the exact format of the source.

Dinoguy1000 (talkcontribs)

The extension does display the text around matches, exactly as it appears on pages; you should double-check your pages to be sure the capitalization is not actually present on them.

To work around this, check the "Use regular expressions" box, and replace each letter that might be uppercase or lowercase with square brackets containing both letterforms (so your example would be [Cc]ommedy [Ff]ilmm)

Cojoilustrado (talkcontribs)

Thank you. I'll check it out.

MvGulik (talkcontribs)

One can also enable case-Insensitive searching by starting a regex with "(?i)"

Dinoguy1000 (talkcontribs)

Oh, that's useful, I didn't know about that one.

Reply to "Caps"

<strike>{n,m} quantifier (?)</strike> Lazy vs Greedy (Default)

MvGulik (talkcontribs)

Did not know "Replace Text" supported the {n,m} quantifier.
Well at least the {n} version that is.
When trying out the {n,m} format like in "{2,}" or "{2,99}" it just matched 2 cases, while ignoring the trailing rest. (?)

In case it matters.
Tested it with leading blank-lines removal like in "^\n{2,99}(.*)"

(Not tested {n,m} with additional trailing quantifiers ... don't really see the point of that)
(to get the job done I switched back to "^\n+?(.*)".)

MvGulik (talkcontribs)

Hmmm ... Might be related to the fact that quantifiers in 'Replace_Text' seem to be set to be lazy by default. (which I keep generally forgetting as I learned it the other way around)

Guess I need to do some additional reading & testing on this.

MvGulik (talkcontribs)

Yea, it is as expected.

RE: "a{4,8}" => "aaaaaabbbaaaaaacccaaaaaa" <= lazy mode.

RE: "a{4,8}?" => "aaaaaabbbaaaaaacccaaaaaa" <= greedy mode.

So far I don't know of any other RE-implementation that is doing it this way. Even the, on the main page linked, mysql page shows its greedy by default. (no mention of "greedy" or "lazy" though)

Although defaulting to RE-lazy-behavior for MW might not be a bad thing ...

(Makes testing 'Replace Text' intended RE code on sites like https://regexr.com/ a bit awkward, and error prone, though)

I do think this 'Lazy-Default vs Greedy' should be mentioned(!) on the 'Replace text' main page.

Reply to "<strike>{n,m} quantifier (?)</strike> Lazy vs Greedy (Default)"

Unexpected behavior(s) - Page moving.

MvGulik (talkcontribs)

Somewhat unexpected behavior(s) in my view.

1) When changing some text that was in one page-case in both the title and the same (file)page content (which was a file-redirect page). The page was moved first, but the content change was skipped. (presuming the initial page move prevented the content change. Although I figure point 2 could also have something to do with it)

2) In the same replacement job(non scripted) I'm pretty sure I disable the "Save the old titles as redirects to the new titles" option. But I did end up with having old-title-redirects on all moved (file)pages. ... Which in this case, if the content change in point 1 would have succeeded would have created a redirection loop.

All in all thing worked out ok (only a few pages had to be changed), but it also leaves some concerns when it comes to doing potential complicated/troublesome changes on large sets of pages.

?) Are there some know problems, or undocumented behaviors, with the "Save the old titles as redirects to the new titles" option ?

Oops: +Local versions.
- MediaWiki 1.36.1
- Replace Text 1.4.1

Reply to "Unexpected behavior(s) - Page moving."

ReplaceText exits with "does not hold regular wikitext"

Cavila (talkcontribs)

Sometimes I need to run ReplaceText on namespaces with content models other than wikitext. That used to work for me if I remember well, but the last time I ran it (a plain-text model in this case), it exited with an error message along the lines of "Wiki page ... does not hold regular wikitext".

Reply to "ReplaceText exits with "does not hold regular wikitext""

Regular expression and special characters

3 (talkcontribs)

In general support for using complex regular expressions seems a bit lacking. I'm trying to do a replacement for <data table="Games">[\s\S]*?<\/data> which utterly fails. But even simpler things don't work as expected. As a simple example, I tried to replace a string with special characters, namely </data> with |}}. However, no replacement I try works. If I escape the special characters, i.e. replace <\/data> with \|\}\}, I end up literally having \|\}\} in the text. If I don't escape the special characters I keep getting the awful Wikimedia\Rdbms\DBQueryError message. Any idea what's wrong?

MW 1.30.1, PHP 7.1.33, MySQL 5.7.38, ReplaceText 1.2 (REL1_30) 07:09, 25 July 2022 (UTC)

Dinoguy1000 (talkcontribs)

Have you tried escaping the < and >? I don't remember for sure if it was S:ReplaceText, but I know I've had some trouble with these characters in some flavor of regex, so that'd be the first thing I'd try. (talkcontribs)

~~ I did. That was not the problem. I verified it by using AAA as the replacement string, which worked.

Reply to "Regular expression and special characters"
SimcaCZE (talkcontribs)

Hi, How I change "<ref>*</ref>" to ""??

Keyacom (talkcontribs)

I see you're looking for the regex that matches any text inside <ref> tags, which also includes both the opening and closing tags.

It's /<ref>[\s\S]*?<\/ref>/ (without the leading or trailing slashes):

  • \/ is intepreted as a literal /
  • [\s\S]* is intepreted as at least 0 characters, including newlines. ? makes the * quantifier lazy (so it matches as least characters as possible).
SimcaCZE (talkcontribs)

Thansk, but it is not working. <ref>*</ref> I have more in article. This sequense delete text in first <ref> and last </ref>

Keyacom (talkcontribs)

That's what the ? modifier to make the * quantifier ungreedy is for. Remove the question mark just in case, I think this uses PCRE's U flag, which makes quantifiers ungreedy by default.

SimcaCZE (talkcontribs)

Hi, I correct code. It is Ok. It is making, what I want. Thanks for help.

Reply to "Change <ref>*</ref> to """

Missing --category option in command-line script

Manu.wikidebats (talkcontribs)

A very useful feature is to replace strings in pages belonging to a given category. This feature exists in Special:Replace text page, but it does not in the command-line script. Why not?

Would it be possible to get a --category parameter for replaceAll.php?

Reply to "Missing --category option in command-line script"

Bug in the replaceAll.php script using the --replacements option

Megajoule (talkcontribs)

MW1.37.2 / ReplaceText 1.7

If one of the lines in the file entered as a parameter to the --replacements option does not result in a replacement, the following lines are ignored even if they potentially result in replacements.

Reply to "Bug in the replaceAll.php script using the --replacements option"

Error at Replace Text special page

Fokebox (talkcontribs)

Hi! I have updated my wiki to MW 1.37.1 and extension itself. After that I see following error at Replace Text special page:

Deprecated: Use of MWNamespace::getSubject was deprecated in MediaWiki 1.34. [Called from MediaWiki\Extension\ReplaceText\SpecialReplaceText::namespaceTables in /var/www/u1406315/data/www/wikireshebnik.org/extensions/ReplaceText/src/SpecialReplaceText.php at line 601] in /var/www/u1406315/data/www/wikireshebnik.org/includes/debug/MWDebug.php on line 375

How can I fix this problem?

Ciencia Al Poder (talkcontribs)

Unless you're a developer, you should disable E_DEPRECATED and E_USER_DEPRECATED notices on production websites

Fokebox (talkcontribs)

I have asked my hosting provider switch off such kinds of mistake, despite it was done I still see this error.

Yaron Koren (talkcontribs)

You should upgrade to the latest version of Replace Text, and then this problem will go away. Don't use the REL1_37 branch (or any other "REL" branch) of Replace Text - it will lead to problems like this.

Reply to "Error at Replace Text special page"