search replace regex question

Hi,

I need to run a search and replace on a large database,
what I need to change is all instances of

#### (eg. 1764 or 1964)
to
(####) (eg. (1764) or (1964))

But there might be other numbers in there such as

(1764) and I do not need those changed to ((1764))

How can I accomplish this?

What kind of database?

Hi,
This is for a mediawiki site, we will use a regex search and replace function to execute the command,
it is an extension that automatically runs the string on the pages, so not directly the whole database

Again, which database?

(or do you want to export the wiki, make the changes, reimport)?

And ist this date only in the article text or also in the Article name, edit comments, etc?

Hi,

The database is mysql, but the regex I will run will not actually be run on the database as such,

mediawiki allows me to run the search and replace on only the articles and titles directly through an extension, so I wont run this on a dump or in mysql itself,
Here is the description from the author of the extension

The set of regular expressions allowed is basically a small subset of the PHP and MySQL regular-expression sets (it has only been tested with MySQL - whether it works on other database systems is unknown). The characters that one can use in the search string are "( ) . * + ? [ ] |", and within the replacement string one can use values like $1, $2 etc. This section will not give a tutorial on using regular expressions (the Wikipedia article is a good place to start for that, as is this page on MySQL regexps), but here is the basic example listed in the inline explanation:
Search string: a(.)c
Replacement string: ac$1
This would look for pages containing the letter 'a', the letter 'c', and any text in between (signified by the ".
"). It would then put that middle text after the 'a' and 'c' - the "$1" in the replacement string refers to the first element of the search string contained within parentheses (in this case, there's only one).

Cheers

Well, I suppose this depends on which subset of the regular expressions the extension writer decided to use. The ones used by PHP vary, and the ones used by MySQL are POSIX compliant. So we'll try a POSIX compliant version:

Search : \(([0-9]{4})\)|([0-9]{4})
Replace: ($1)

But it's difficult to say if it will work correctly or not.

1 Like

In unix,to find a string in a file and replace it with another string, you can use the below procedure
"
Open the file with 'vi' editor.
The last line can be specified as a dollar sign ($). To search and replace from the current line through the last line use the command:

:.,$s/up/right/ <'up' is the term to be found and 'right' is the term to be replaced.>

@lawstudent: Is this what you're looking for?

inputfile.txt:

#### (eg. 1764 or 1964)
#### (eg. 1864 or 2064)
#### (eg. 1964 or 1764)
#### (eg. (2764) or (1164))
$ perl -ne 's/ ([0-9]{4})/ \($1\)/g; print $_' inputfile.txt
#### (eg. (1764) or (1964))
#### (eg. (1864) or (2064))
#### (eg. (1964) or (1764))
#### (eg. (2764) or (1164))