472,995 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,995 software developers and data experts.

replacing string with regexp

Hi,

I would like to replace all strings in a table with regexp:

the strings contain the substring "-na", and I would like to replace the
whole table field with the original content but without the substring "-
na".

I know already how to find the lines that contain the "-na" with LIKE
and/or REGEXP, but i dont know how to replace it.

any help possible here?

thanks

Oliver

Jul 19 '05 #1
6 10479
Oliver Spiesshofer wrote:
I would like to replace all strings in a table with regexp:
the strings contain the substring "-na", and I would like to replace the
whole table field with the original content but without the substring "-
na".
I know already how to find the lines that contain the "-na" with LIKE
and/or REGEXP, but i dont know how to replace it.

I am not aware of an update statement that could achieve this in one
query.
The typical solution for this kind of job is to write a routine in
your api language. Here is a perl approach:

----------------------------------------------------

#!/usr/bin/perl
use DBI;

# walk through the rows of mytable
$readdb=DBI->connect("DBI:mysql:your_db:your.host.com","userna me","pass");
$readquery=$readdb->prepare("SELECT ID,myfield FROM mytable");
$readquery->execute;
$readnumrows = $readquery->rows;

# for each row found, read the value to be replaced
# and perform an update query
while (@readarray = $readquery->fetchrow_array)
{
# assign vars
$ID=$readarray[0];
$newvalue=$readarray[1];

# perform the regexp in perl
$newvalue=~s/\-na//i;

# update this row
$updatedb=DBI->connect("DBI:mysql:your_db:your.host.com","userna me","pass");
$updatequery=$updatedb->prepare("UPDATE mytable SET
myfield='$newvalue' WHERE ID='$ID'");
$updatequery->execute;

# end of this query
$updatequery->finish;
$updatedb->disconnect;
}

# end of main read query
$readquery->finish;
$readdb->disconnect;

# report
print "done.\n";

----------------------------------------------------

If you are not a perl man, the same routine can be made in php, vb...

Hope this could be useful.
Bart
Jul 19 '05 #2
Oliver Spiesshofer wrote:
I would like to replace all strings in a table with regexp:
the strings contain the substring "-na", and I would like to replace the
whole table field with the original content but without the substring "-
na".
I know already how to find the lines that contain the "-na" with LIKE
and/or REGEXP, but i dont know how to replace it.

I am not aware of an update statement that could achieve this in one
query.
The typical solution for this kind of job is to write a routine in
your api language. Here is a perl approach:

----------------------------------------------------

#!/usr/bin/perl
use DBI;

# walk through the rows of mytable
$readdb=DBI->connect("DBI:mysql:your_db:your.host.com","userna me","pass");
$readquery=$readdb->prepare("SELECT ID,myfield FROM mytable");
$readquery->execute;
$readnumrows = $readquery->rows;

# for each row found, read the value to be replaced
# and perform an update query
while (@readarray = $readquery->fetchrow_array)
{
# assign vars
$ID=$readarray[0];
$newvalue=$readarray[1];

# perform the regexp in perl
$newvalue=~s/\-na//i;

# update this row
$updatedb=DBI->connect("DBI:mysql:your_db:your.host.com","userna me","pass");
$updatequery=$updatedb->prepare("UPDATE mytable SET
myfield='$newvalue' WHERE ID='$ID'");
$updatequery->execute;

# end of this query
$updatequery->finish;
$updatedb->disconnect;
}

# end of main read query
$readquery->finish;
$readdb->disconnect;

# report
print "done.\n";

----------------------------------------------------

If you are not a perl man, the same routine can be made in php, vb...

Hope this could be useful.
Bart
Jul 19 '05 #3
Oliver Spiesshofer wrote:
I would like to replace all strings in a table with regexp:
the strings contain the substring "-na", and I would like to replace the
whole table field with the original content but without the substring "-
na".
I know already how to find the lines that contain the "-na" with LIKE
and/or REGEXP, but i dont know how to replace it.

I am not aware of an update statement that could achieve this in one
query.
The typical solution for this kind of job is to write a routine in
your api language. Here is a perl approach:

----------------------------------------------------

#!/usr/bin/perl
use DBI;

# walk through the rows of mytable
$readdb=DBI->connect("DBI:mysql:your_db:your.host.com","userna me","pass");
$readquery=$readdb->prepare("SELECT ID,myfield FROM mytable");
$readquery->execute;
$readnumrows = $readquery->rows;

# for each row found, read the value to be replaced
# and perform an update query
while (@readarray = $readquery->fetchrow_array)
{
# assign vars
$ID=$readarray[0];
$newvalue=$readarray[1];

# perform the regexp in perl
$newvalue=~s/\-na//i;

# update this row
$updatedb=DBI->connect("DBI:mysql:your_db:your.host.com","userna me","pass");
$updatequery=$updatedb->prepare("UPDATE mytable SET
myfield='$newvalue' WHERE ID='$ID'");
$updatequery->execute;

# end of this query
$updatequery->finish;
$updatedb->disconnect;
}

# end of main read query
$readquery->finish;
$readdb->disconnect;

# report
print "done.\n";

----------------------------------------------------

If you are not a perl man, the same routine can be made in php, vb...

Hope this could be useful.
Bart
Jul 19 '05 #4
ba**@nijlen.com (Bart Van der Donck) wrote in
news:b5**************************@posting.google.c om:
Oliver Spiesshofer wrote:
I know already how to find the lines that contain the "-na" with LIKE
and/or REGEXP, but i dont know how to replace it.

I am not aware of an update statement that could achieve this in one
query.
The typical solution for this kind of job is to write a routine in
your api language. Here is a perl approach:

If you are not a perl man, the same routine can be made in php, vb...


thanks a lot. I was hoping to be able to use the %1... syntax of the regexp
also direclty in the update statement to reflect the results. what a pity
it does not work.

I will do it in PHP then, thanx!

Oliver

Jul 19 '05 #5
ba**@nijlen.com (Bart Van der Donck) wrote in
news:b5**************************@posting.google.c om:
Oliver Spiesshofer wrote:
I know already how to find the lines that contain the "-na" with LIKE
and/or REGEXP, but i dont know how to replace it.

I am not aware of an update statement that could achieve this in one
query.
The typical solution for this kind of job is to write a routine in
your api language. Here is a perl approach:

If you are not a perl man, the same routine can be made in php, vb...


thanks a lot. I was hoping to be able to use the %1... syntax of the regexp
also direclty in the update statement to reflect the results. what a pity
it does not work.

I will do it in PHP then, thanx!

Oliver

Jul 19 '05 #6
ba**@nijlen.com (Bart Van der Donck) wrote in
news:b5**************************@posting.google.c om:
Oliver Spiesshofer wrote:
I know already how to find the lines that contain the "-na" with LIKE
and/or REGEXP, but i dont know how to replace it.

I am not aware of an update statement that could achieve this in one
query.
The typical solution for this kind of job is to write a routine in
your api language. Here is a perl approach:

If you are not a perl man, the same routine can be made in php, vb...


thanks a lot. I was hoping to be able to use the %1... syntax of the regexp
also direclty in the update statement to reflect the results. what a pity
it does not work.

I will do it in PHP then, thanx!

Oliver

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sorby | last post by:
Hi I've been coding in PHP for a little while now and was starting to feel pretty confident but then realise I need to understand regular expressions to solve a particular problem I've got ......
10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
3
by: DrewM | last post by:
I'm sure this isn't difficult, but it's Friday afternoon (!). I'm trying to use a regular expression to match html tags in a string and convert them to lower case. It's the RegExp object that I'm...
0
by: Oliver Spiesshofer | last post by:
Hi, I would like to replace all strings in a table with regexp: the strings contain the substring "-na", and I would like to replace the whole table field with the original content but without...
3
by: Arjen | last post by:
Hello, I have inside a string a complete url. http://www.somedomain.com/index.php?action=shownewsitem&id=125 At the end you see "id=", the id can be "1", "23", "234", etc.. Now I want to...
0
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
2
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
14
by: Adnan Siddiqi | last post by:
Hi Suppose I have following URLs comming from an HTML document <a href="http://mydomain1.com">Domain1</a> <a...
0
by: deathtospam | last post by:
I have a value, retrieved from a recordset, that contains any number of hyperlinks to a number. For example: <a href="123456">Hi</a<a href='334455'>Hola</a> What I need to do is parse the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.