473,408 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 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 10511
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.