Jump to content

Wikipedia talk:AutoWikiBrowser/Find and replace

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lowercase sigmabot III (talk | contribs) at 03:30, 5 July 2016 (Archiving 1 discussion(s) to Wikipedia talk:AutoWikiBrowser/Find and replace/Archive 1) (bot). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Switch order of two sections

Is there a way I could switch the order of two sections?

For example, I would like to find this:

==Section 1==
*[[Foo Bar]]
*[[Lorem Ipsum]]
*[[Bar Foo]]

==Section 2==
*[[Dolor sit amet]]
*[[Xyzzy]]

and replace it with this:

==Section 2==
*[[Dolor sit amet]]
*[[Xyzzy]]

==Section 1==
*[[Foo Bar]]
*[[Lorem Ipsum]]
*[[Bar Foo]]

I would like to switch the order of two sections, keeping the name of the sections and their content, simply switching the order.

I have tried, but I cannot get it to work. I tried putting .{1,} in the find, below the sections, and $1 and $2 in the replace, but it did not work when I tested it. I also tested with (.*?), but I cannot get it to work. I have little experience with RegEx and would appreciate some help.
PapíDimmi (talk) 00:27, 1 June 2016 (UTC)[reply]

@PapíDimmi: Can you clarify exactly what you want to happen: is it sections of two specific names you want swapped? Will they already be in a particular order? Will they be adjacent? If you could be specific about the use for this regex, that would really help. —  crh 23  (Talk) 20:52, 2 June 2016 (UTC)[reply]
@Crh23: I want to switch the order of two specific sections in articles.
If I am going to be specific, I want the “See also” section, if it is above the “Appearances” section, to move below the “Appearances” section.
For example, I would want to switch this:
==See also==
==Appearances==
with this:
==Appearances==
==See also==
I.e., I would simply like to move the “See also” section below the “Appearances” section on pages where the “See also” section is above the “Appearances” section. If the “See also” section is already below the “Appearances” section, no changes should be made.
PapíDimmi (talk) 21:02, 2 June 2016 (UTC)[reply]
OK, I think I've got something that works. You'll need SingleLine ticked. Replace \s?(== ?See [a|A]lso ?==\s+.*?)\s?(== ?Appearances ?==\s*.*?)\s?(=|\Z) with \n$2\n$1\n$3. To be precise, this takes the Appearances section (iff it is below the See also section) and puts it immediately above the see also section: if the sections are not adjacent it may cause whitespace problems, not sure. —  crh 23  (Talk) 21:33, 2 June 2016 (UTC)[reply]
@Crh23: Thank you, that almost works; however, it removes the space. I would like it to keep the space in-between the two sections.
E.g., it replaced this:
==See also==
*[[lorem]]
*[[ipsum]]
*[[xyzzy]]

==Appearances==
*[[foo]]
*[[bar]]
with this:
==Appearances==
*[[foo]]
*[[bar]]
==See also==
*[[lorem]]
*[[ipsum]]
*[[xyzzy]]


PapíDimmi (talk) 00:11, 3 June 2016 (UTC)[reply]

@PapíDimmi: That was a little tricky to figure out, but I think I've got it. Find (== ?See [a|A]lso ?==\s+.*?)(\s*)(== ?Appearances ?==\s+.*?)(\s*(?:=|\Z)) and replace with $3$2$1$4, again with SingleLine ticked. What this does (I hope!) is swap the sections around, ignoring whitespace at the end, keeping the whitespace between them in place. —  crh 23  (Talk) 08:45, 3 June 2016 (UTC)[reply]
@Crh23: That works superbly! Thank you for all the help you have given me!
PapíDimmi (talk) 10:13, 3 June 2016 (UTC)[reply]
@Crh23: Hello. I, unfortunately, encountered an error. In articles with the template Interlang, it removes Interlang’s parameters and puts them below the “See also” section. Here is a screenshot. As you can see in the screenshot, it moved the “See also” into the Interlang template, making it a parameter of the template; it does not show up on the page and nor does the Interlang template’s actual parameters. Could you please help me keep this error from happening? Thank you.
PapíDimmi (talk) 20:41, 4 June 2016 (UTC)[reply]
I also encountered AutoWikiBrowser move reference names and templates below the “See also” and “Appearances” sections. If you cannot fix these errors, that is fine; I will stop using the RegEx code. However, if you can, I would really appreciate it.
PapíDimmi (talk) 21:14, 4 June 2016 (UTC)[reply]
@PapíDimmi: Using (== ?See [a|A]lso ?==\s+.*?)(\s*)(== ?Appearances ?==\s+.*?)(\s*(?:==|\Z)) I think fixes those errors: the problem is that the regex was treating = as the start of a new section (I didn't account for its use in templates, now it only treats == as a new section. There still may be some situations where the find and replace will produce errors (use of <nowiki>...</nowiki>, use of single equals headings, etc.): these are unavoidable without making it significantly more complicated. —  crh 23  (Talk) 10:14, 5 June 2016 (UTC)[reply]
Thanks, I will try that.
PapíDimmi (talk) 21:52, 5 June 2016 (UTC)[reply]
@Crh23: It seems to work nicely, except it moves the “See also” section below the Interlang template. In an article, there was a See also section, Appearances section, and an Interlang template. Once AutoWikiBrowser edited it, there was an Appearances section, Interlang template, and See also section. The Interlang template should be at the bottom and not in the middle of the two sections. Is there a way to fix this? Sorry if I am asking for too much, but I would be really glad if you could help me with this, and hopefully there would be no more problems with it.
PapíDimmi (talk) 03:47, 7 June 2016 (UTC)[reply]
() @PapíDimmi: What is the form of the Interlang template, is it {{Interlang}}? If so, (== ?See [a|A]lso ?==\s+.*?)(\s*)(== ?Appearances ?==\s+.*?)(\s*(?:==|\Z|\{\{[Ii]nterlang\}\})) should work — it treats the interlang template as another way to end the appearances section. —  crh 23  (Talk) 13:23, 7 June 2016 (UTC)[reply]
Thanks. I will try that.
PapíDimmi (talk) 15:55, 7 June 2016 (UTC)[reply]
@Crh23: Hello. Unfortunately, it does not work.
It replaced this:
==See also==
*[[Foo Bar]]

==Appearances==
*[[Lorem ipsum]] {{co}}

{{Interlang
| es = Foobar
}}
with this:
==Appearances==
*[[Lorem ipsum]] {{co}}

{{Interlang
| es = Foobar
}}

==See also==
*[[Foo Bar]]
I tested without the {{co}} template, it did the same thing, so that template is not the cause. I want the Interlang template to be at the bottom, where it already was.
PapíDimmi (talk) 00:54, 9 June 2016 (UTC)[reply]
Right, so the template is not just of the form {{Interlang}}. (== ?See [a|A]lso ?==\s+.*?)(\s*)(== ?Appearances ?==\s+.*?)(\s*(?:==|\Z|\{\{[Ii]nterlang.*?\}\})) will fix that I think, not tested much though.—  crh 23  (Talk) 10:08, 9 June 2016 (UTC)[reply]
Oh, my apologies; I forgot to mention that {{Interlang}} has parameters (I thought that you already knew that). Thank you for helping me. I will try the code provided and get back to you if it works or does not.
PapíDimmi (talk) 20:22, 9 June 2016 (UTC)[reply]
We don't use it on enwiki - it's all done through wikidata. I'm not sure why the template exists here actually. The only link to it is this discussion.
— Preceding unsigned comment added by Crh23 (talkcontribs) 09:52, 10 June 2016 (UTC)[reply]
@Crh23: I found a problem.
It replaced this:
==See also==
*Bar
==Appearances==
*Foo

[[Category:Foobar]]
with this:
==Appearances==
*Foo

[[Category:Foobar]]

==See also==
*Bar
I want it to replace it with this:
==Appearances==
*Foo

==See also==
*Bar

[[Category:Foobar]]
I would be very happy if you could help me with this! Thank you.
PapíDimmi (talk) 17:10, 12 June 2016 (UTC)[reply]
Added categories as another way to end the last section, (== ?See [a|A]lso ?==\s+.*?)(\s*)(== ?Appearances ?==\s+.*?)(\s*(?:==|\Z|\{\{[Ii]nterlang.*?\}\}|\[\[[cC]ategory:)), not tested, other settings as before. —  crh 23  (Talk) 21:55, 15 June 2016 (UTC)[reply]
@Crh23: Thank you! I will try that.
PapíDimmi (talk) 14:49, 16 June 2016 (UTC)[reply]

@Crh23: It did not work as intended.

It replaced this:

==See also==
*Bar
==Appearances==
*Foo

[[Category:Foobar]]

with this:

==Appearances==
*Foo
==See also==
*Bar

[[Category:Foobar]]

There should be a space in-between the sections, like this:

==Appearances==
*Foo

==See also==
*Bar

[[Category:Foobar]]

Could you help me fix that? Thank you.
PapíDimmi (talk) 05:20, 19 June 2016 (UTC)[reply]

@PapíDimmi: that problem was in the original text, so was transferred to the new text. The best way to fix this is a second regex, which will insert a blank line between all sections that don't have one. Find (?<=[^\n\r])(\n)(=+)([^=\n]*?)\2(\n) and replace with \n\n$2$3$2\n (not tested much). —  crh 23  (Talk) 08:22, 19 June 2016 (UTC)[reply]
That works. Thank you!
PapíDimmi (talk) 08:24, 19 June 2016 (UTC)[reply]
I discovered a little problem: I do not want it to insert a space after an empty section.
E.g., it replaces this:
==Foo==
===Bar===
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
with this:
==Foo==

===Bar===
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
I want there to be spaces in-between sections but not sections which are empty and are followed by another section instead. Therefore, I do not want it to replace the above example text with anything. Is there a workaround for this? Thank you.
PapíDimmi (talk) 09:32, 19 June 2016 (UTC)[reply]
@PapíDimmi: Easy fix: (?<=[^\n\r=])(\n)(=+)([^=\n]*?)\2(\n). —  crh 23  (Talk) 13:15, 19 June 2016 (UTC)[reply]
That works. Thank you!
PapíDimmi (talk) 13:23, 19 June 2016 (UTC)[reply]

Add spaces to headings

Hi!

I would like to replace ==Foobar== with == Foobar ==, where “Foobar” is any string of characters (aka any word/words, symbol/symbols, number/numbers, etc.).

Example:
Replace this:

==Amazing Header of 2016==
This is some sort of section.
Indeed, it is.

with this:

== Amazing Header of 2016 ==
This is some sort of section.
Indeed, it is.

I would also like the same to apply to ===Foobar=== and ====Foobar====.
PapíDimmi (talk) 19:05, 15 June 2016 (UTC)[reply]

Is this for the English WP? Mr Stephen (talk) 19:43, 15 June 2016 (UTC)[reply]
@Mr Stephen: it's not - PapiDimmi doesn't have AWB rights on enwiki, and their previous edits aren't to enwiki. —  crh 23  (Talk) 20:04, 15 June 2016 (UTC)[reply]
@PapiDimmi: First, make sure this is desirable on the wiki you are working on — here on enwiki mass changing this would be forbidden, articles need only be consistent within themselves. If it is acceptable, try replacing (\s)(=+)\s*(\S.*?\S)\s*\2(\s) with $1$2 $3 $2$4. —  crh 23  (Talk) 20:04, 15 June 2016 (UTC)[reply]
Yes, it is desirable—in fact, I was told to.
PapíDimmi (talk) 14:49, 16 June 2016 (UTC)[reply]
Nah, that won't work. (\s)(=+)(\S.*?\S)\2(\s) seems OK. Mr Stephen (talk) 21:29, 15 June 2016 (UTC)[reply]
Aren't we adding spaces? —  crh 23  (Talk) 21:50, 15 June 2016 (UTC)[reply]
Indeed we are, but what if they are already there? Mr Stephen (talk) 21:54, 15 June 2016 (UTC)[reply]
I assume then we will leave as is? —  crh 23  (Talk) 21:56, 15 June 2016 (UTC)[reply]
Your regex will match several '=' followed by a space, as both '\S' and '.' match a '='. Mr Stephen (talk) 23:03, 15 June 2016 (UTC)[reply]
Good point, but yours doesn't fix that: both (\s)(=+)\s*(\S.*?\S)\s*\2(\s) and (\s)(=+)(\S.*?\S)\2(\s) match ==== . That said yours may be slightly more useful if we want to fix headings that have too many spaces. With that in mind, I propose (\s)(=+)\s*([^\s=].*?[^\s=])\s*\2(\s). —  crh 23  (Talk) 09:51, 16 June 2016 (UTC)[reply]
@Mr Stephen @Crh23: Thanks for your help! @Crh23: It seems to work nicely!
PapíDimmi (talk) 14:49, 16 June 2016 (UTC)[reply]

A rule does not work like I want it to

Here are a rule I have created in advanced find and replace:

Find: {{wikipedia|%%pagename%%}}
Replace: {{wikipedia}}

(I also tried with %%title%% and %%basepagename%% but to no avail.)

Let us say that an article titled “Foobar” has the following therein: {{wikipedia|Foobar}}

I want and expect my rule to replace that with simply {{wikipedia}}, but AutoWikiBrowser makes no change to the page.

I have the “Regular expression” box ticked off, by the way. When I tick it on, it replaces {{wikipedia|Foobar}} with {{wikipedia}}|{{wikipedia}}, which is, obviously, not what I want.

To sum it up: I want to remove the parameter of the {{wikipedia}} template in articles but only if the parameter is the same as the title of the article.

Can anyone please please help me with this?
PapíDimmi (talk) 00:09, 25 June 2016 (UTC)[reply]

I don't know if %%pagename%% is supposed to not work without regex, but if you use regex, you need to "escape" special characters in the "Find" string by adding backslashes: \{\{wikipedia\|%%pagename%%\}\}. SiBr4 (talk) 14:24, 25 June 2016 (UTC)[reply]
(My apologies for the late reply.) That works perfectly. Thank you!
PapíDimmi (talk) 15:58, 4 July 2016 (UTC)[reply]

Switch order of two fields

Is there a way I could switch the order of two sections?

For example, I would like to find this:

| source = {{own}}
| date = 2009-01-01

and replace it with this:

| date = 2009-01-01
| source = {{own}}

I would like to switch the order of these two fields sections, keeping the name of the sections and their content, simply switching the order (so the date the source remains, but the order changes).

My reason for this is due to the "date" field displaying before the "source" field in Wikimedia Commons file descriptions.

Thanks, OSX (talkcontributions) 04:39, 29 June 2016 (UTC)[reply]

What's the context? I assume this is within a particular template, which? —  crh 23  (Talk) 09:44, 29 June 2016 (UTC)[reply]
It is for Commons:Template:Information. Thanks, OSX (talkcontributions) 12:03, 29 June 2016 (UTC)[reply]
Will the options always be adjacent? —  crh 23  (Talk) 12:47, 29 June 2016 (UTC)[reply]
Yes. OSX (talkcontributions) 13:06, 29 June 2016 (UTC)[reply]
Find ({{Information.*?)(\s?\|\s*?source\s*?=.*?)(\s?\|\s*?date\s*?=.*?)(\s?\|) and replace with $1$3$2$4, with SingleLine ticked. I haven't tested this much, and there will probably be cases where it does an incorrect replacement, but it should work usually. Make sure to check your edits! —  crh 23  (Talk) 14:05, 29 June 2016 (UTC)[reply]
Also, check that the change is welcome: it is a very minor change, so it might be unwelcome to make it on its own. —  crh 23  (Talk) 14:06, 29 June 2016 (UTC)[reply]
Thanks for the code. For me though, it does not work. I have copied your "find" code into the "find" field and $1$3$2$4 into "replace with", with SingleLine ticked. OSX (talkcontributions) 14:24, 29 June 2016 (UTC)[reply]
Do you have Regex ticked too? —  crh 23  (Talk) 14:42, 29 June 2016 (UTC)[reply]
I do now and that fixed it! Thank you very much for your help :) OSX (talkcontributions) 14:50, 29 June 2016 (UTC)[reply]
You're welcome! Sorry, forgot to mention it (doesn't show in the regex tester tool, for obvious reasons). —  crh 23  (Talk) 16:11, 29 June 2016 (UTC)[reply]

Move bold

I want to replace this:

'''Any words (born {{birthdate|any number|any number|any number}})'''

with this:

'''Any words''' (born {{birthdate|any number|any number|any number}})

I want to keep the words and numbers, but move the bold so that the only the “Any words” at the start is bold and not the whole thing.

I tried this:
Find:

'''(.*) (born {{birthdate|(.*)|(.*)|(.*)}})'''

Replace:

'''$1''' (born {{birthdate|$2|$3|$4}})

…but that did not work.

Example

Replace this:

'''John Smith (born {{birthdate|1973|12|05}})'''

with this:

'''John Smith''' (born {{birthdate|1973|12|05}})

Could anyone please help?
PapíDimmi (talk) 23:36, 4 July 2016 (UTC)[reply]