replacing string with regexp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oliver Spiesshofer

    #1

    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

  • Bart Van der Donck

    #2
    Re: replacing string with regexp

    Oliver Spiesshofer wrote:
    [color=blue]
    > 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.[/color]


    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:m ysql:your_db:yo ur.host.com","u sername","pass" );
    $readquery=$rea ddb->prepare("SELEC T 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=$read array[1];

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

    # update this row
    $updatedb=DBI->connect("DBI:m ysql:your_db:yo ur.host.com","u sername","pass" );
    $updatequery=$u pdatedb->prepare("UPDAT E mytable SET
    myfield='$newva lue' 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

    Comment

    • Bart Van der Donck

      #3
      Re: replacing string with regexp

      Oliver Spiesshofer wrote:
      [color=blue]
      > 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.[/color]


      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:m ysql:your_db:yo ur.host.com","u sername","pass" );
      $readquery=$rea ddb->prepare("SELEC T 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=$read array[1];

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

      # update this row
      $updatedb=DBI->connect("DBI:m ysql:your_db:yo ur.host.com","u sername","pass" );
      $updatequery=$u pdatedb->prepare("UPDAT E mytable SET
      myfield='$newva lue' 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

      Comment

      • Bart Van der Donck

        #4
        Re: replacing string with regexp

        Oliver Spiesshofer wrote:
        [color=blue]
        > 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.[/color]


        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:m ysql:your_db:yo ur.host.com","u sername","pass" );
        $readquery=$rea ddb->prepare("SELEC T 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=$read array[1];

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

        # update this row
        $updatedb=DBI->connect("DBI:m ysql:your_db:yo ur.host.com","u sername","pass" );
        $updatequery=$u pdatedb->prepare("UPDAT E mytable SET
        myfield='$newva lue' 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

        Comment

        • Oliver Spiesshofer

          #5
          Re: replacing string with regexp

          bart@nijlen.com (Bart Van der Donck) wrote in
          news:b5884818.0 401140257.5264b 9fc@posting.goo gle.com:
          [color=blue]
          > Oliver Spiesshofer wrote:
          >[color=green]
          >> 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.[/color]
          >
          >
          > 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...
          >[/color]

          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

          Comment

          • Oliver Spiesshofer

            #6
            Re: replacing string with regexp

            bart@nijlen.com (Bart Van der Donck) wrote in
            news:b5884818.0 401140257.5264b 9fc@posting.goo gle.com:
            [color=blue]
            > Oliver Spiesshofer wrote:
            >[color=green]
            >> 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.[/color]
            >
            >
            > 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...
            >[/color]

            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

            Comment

            • Oliver Spiesshofer

              #7
              Re: replacing string with regexp

              bart@nijlen.com (Bart Van der Donck) wrote in
              news:b5884818.0 401140257.5264b 9fc@posting.goo gle.com:
              [color=blue]
              > Oliver Spiesshofer wrote:
              >[color=green]
              >> 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.[/color]
              >
              >
              > 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...
              >[/color]

              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

              Comment

              Working...