Regular expression problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Ray

    #1

    Regular expression problem

    I am trying to come up with a regular expression that searches and
    replaces all occurences of the string "'" and replaces it with "''" .
    However if a backslash comes before the single quote I do NOT want it
    too be replaced with two single quotes. Any clues?

    Joe
  • Tim Orbaker

    #2
    Re: Regular expression problem



    Joe Ray wrote:[color=blue]
    > I am trying to come up with a regular expression that searches and
    > replaces all occurences of the string "'" and replaces it with "''" .
    > However if a backslash comes before the single quote I do NOT want it
    > too be replaced with two single quotes. Any clues?
    >
    > Joe[/color]

    Try:

    public class regexDemo {
    private static void show( String s )
    {
    System.out.prin tln( "IN [" + s + "]" );
    s = s.replaceAll( "([^\\\\])\'", "$1\'\'" );
    System.out.prin tln( "OUT[" + s + "]\n" );

    return;
    }

    public static void main( String args[] )
    {
    show( "This 'string' needs quoting." );
    show( "And 'quote' some of these but don\\'t do all." );

    return;
    }
    }

    Tim

    Comment

    • D

      #3
      Re: Regular expression problem

      raymis@hotmail. com (Joe Ray) wrote in message news:<46039644. 0407220646.efbd 859@posting.goo gle.com>...[color=blue]
      > I am trying to come up with a regular expression that searches and
      > replaces all occurences of the string "'" and replaces it with "''" .
      > However if a backslash comes before the single quote I do NOT want it
      > too be replaced with two single quotes. Any clues?
      >
      > Joe[/color]


      [^\\]'

      Comment

      • Chris Dutton

        #4
        Re: Regular expression problem

        Joe Ray wrote:[color=blue]
        > I am trying to come up with a regular expression that searches and
        > replaces all occurences of the string "'" and replaces it with "''" .
        > However if a backslash comes before the single quote I do NOT want it
        > too be replaced with two single quotes. Any clues?[/color]

        A zero-width look-ahead assertion should be able to do what you need.

        (?<=[^\\]|.{0})'

        Comment

        • Raymond DeCampo

          #5
          Re: Regular expression problem

          Joe Ray wrote:[color=blue]
          > I am trying to come up with a regular expression that searches and
          > replaces all occurences of the string "'" and replaces it with "''" .
          > However if a backslash comes before the single quote I do NOT want it
          > too be replaced with two single quotes. Any clues?
          >
          > Joe[/color]

          Joe,

          <mind-reading-mode>
          If you are doing this for the purpose of creating an SQL string, you
          will be better off in the long run to use a PreparedStateme nt. E.g.

          Connection conn = getConnection() ;
          PreparedStateme nt pStmt = conn.prepareSta tement(
          "SELECT id FROM My_Table WHERE name = ?");
          pStmt.setString (1, "Joe's Pizza");
          ResultSet rs = pStmt.execute() ;

          Then it is the JDBC driver's problem to worry about escaping strings.
          </mind-reading-mode>

          HTH,
          Ray

          --
          XML is the programmer's duct tape.

          Comment

          • IchBin

            #6
            Re: Regular expression problem


            I do something like this for a jdbc insert:

            private int insertDetailSta tement() {
            /**
            * Buid SQL Data Statement
            */
            int fKeyDetail = checkDupDetail( );
            if (fKeyDetail > 0) {
            return fKeyDetail;
            };
            /**
            * Build the author_detail insert STATEMENT with data
            */
            String sqlStatement = DETAILINSERTSQL
            + jTitle.getText( ).trim().replac eAll("'", "''") + "', '"
            + jSuffix.getText ().trim().repla ceAll("'", "''") + "', '"
            + jLastName.getTe xt().trim().rep laceAll("'", "''") + "', '"
            + jMiddleName.get Text().trim().r eplaceAll("'", "''") + "', '"
            + jFirsfName.getT ext().trim().re placeAll("'", "''") + "', '"
            + jBirthDeath.get Text() + "', '"
            + jdescription.ge tText().trim(). replaceAll("'", "''") + "', '"
            + jLink.getText() + "')";
            /**
            * Insert the Author Data
            */
            JPeopleQuotesAp pDB.insertTable Row(sqlStatemen t);

            return checkDupDetail( );
            "Raymond DeCampo" <rdecampo@spam. twcny.spam.rr.s pam.com.spam> wrote in
            message news:_3BMc.5736 3$yd5.28774@twi ster.nyroc.rr.c om...[color=blue]
            > Joe Ray wrote:[color=green]
            > > I am trying to come up with a regular expression that searches and
            > > replaces all occurences of the string "'" and replaces it with "''" .
            > > However if a backslash comes before the single quote I do NOT want it
            > > too be replaced with two single quotes. Any clues?
            > >
            > > Joe[/color]
            >
            > Joe,
            >
            > <mind-reading-mode>
            > If you are doing this for the purpose of creating an SQL string, you
            > will be better off in the long run to use a PreparedStateme nt. E.g.
            >
            > Connection conn = getConnection() ;
            > PreparedStateme nt pStmt = conn.prepareSta tement(
            > "SELECT id FROM My_Table WHERE name = ?");
            > pStmt.setString (1, "Joe's Pizza");
            > ResultSet rs = pStmt.execute() ;
            >
            > Then it is the JDBC driver's problem to worry about escaping strings.
            > </mind-reading-mode>
            >
            > HTH,
            > Ray
            >
            > --
            > XML is the programmer's duct tape.[/color]


            Comment

            • Florentin Ionescu

              #7
              Re: Regular expression problem

              Chris Dutton wrote:[color=blue]
              > A zero-width look-ahead assertion should be able to do what you need.
              >[/color]
              (?<=[^\\]|.{0})'
              ^^^^^
              Will you please explain the last part haw it works ?
              Thank you

              Comment

              Working...