Regular Expression Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chuck Haeberle

    #1

    Regular Expression Help

    I have an interesting regular expression challenge for someone more
    experienced with them than I for a data layer class...

    I need an expression to search a SQL statement (any type, SELECT INSERT
    UPDATE OR DELETE) and find all single apostrophes which should be replaced
    with a double apostrophe without affecting the apostrophes used to delimit
    the string values:

    Example:
    SELECT field FROM table WHERE stringfield = 'This is one of Jerry's worst
    examples. Who's going to fix it?' or stringfield2='W here's Wendy's Beef
    Hamburger'
    or
    INSERT INTO table (field1, field2) VALUES ('Wings'n'thing s', 'Who's Fancy
    Restaurant')

    I can get a pattern which matches a single ' within two other ', but get
    hung up when I start trying to cope with multiple ' within the enclosing ',
    and I can't even fathom how to handle the potential for multiple matches...

    Anyone feel like giving it a shot? I really would not want to have to
    figure this out manually nor do I want to force the users of my data layer
    to pre process their strings manually...


  • Paul E Collins

    #2
    Re: Regular Expression Help

    "Chuck Haeberle" <chaeberl@contr actor.nocienasp am.com> wrote:
    [color=blue]
    > I need an expression to search a SQL statement
    > (any type, SELECT INSERT UPDATE OR
    > DELETE) and find all single apostrophes which
    > should be replaced with a double apostrophe
    > without affecting the apostrophes used to delimit
    > the string values:[/color]

    You can't do this unambiguously. Here, for example ...

    SELECT x FROM y WHERE text='something ' OR text='other'

    .... OR might be part of one long string with apostrophes in it.

    A better idea is to pass a set of parameters to your data layer class and
    use them to replace simple placeholders for their locations in the query
    string. That way, you know which parts are parameters and can deal with them
    accordingly.

    void ExecuteSQL(stri ng strQuery, params string[] strParameters);

    My example would then be written:

    MyDataLayer.Exe cuteSQL("SELECT x FROM y WHERE text='?' OR text='?'",
    "something" , "other");
    .... or ...
    MyDataLayer.Exe cuteSQL("SELECT x FROM y WHERE text='?'", "something' OR
    text='other");

    P.

    --



    Comment

    • amby

      #3
      Re: Regular Expression Help

      in vb i used to replace using the replace function
      i guess you could do the same here

      public string Quotesafe(strin g sourcestring )
      {
      return regex.replace(s qlstring, "'", "''")
      }

      but you gotta do it before placing the value inside the single quotes for
      the sql string

      eg. varString = "d'costa"
      "update employee set name = '"+ quotesafe(varst ring) + "'"


      hth



      "Paul E Collins" <find_my_real_a ddress@CL4.org> wrote in message
      news:bptpao$4s8 $1@titan.btinte rnet.com...[color=blue]
      > "Chuck Haeberle" <chaeberl@contr actor.nocienasp am.com> wrote:
      >[color=green]
      > > I need an expression to search a SQL statement
      > > (any type, SELECT INSERT UPDATE OR
      > > DELETE) and find all single apostrophes which
      > > should be replaced with a double apostrophe
      > > without affecting the apostrophes used to delimit
      > > the string values:[/color]
      >
      > You can't do this unambiguously. Here, for example ...
      >
      > SELECT x FROM y WHERE text='something ' OR text='other'
      >
      > ... OR might be part of one long string with apostrophes in it.
      >
      > A better idea is to pass a set of parameters to your data layer class and
      > use them to replace simple placeholders for their locations in the query
      > string. That way, you know which parts are parameters and can deal with[/color]
      them[color=blue]
      > accordingly.
      >
      > void ExecuteSQL(stri ng strQuery, params string[] strParameters);
      >
      > My example would then be written:
      >
      > MyDataLayer.Exe cuteSQL("SELECT x FROM y WHERE text='?' OR text='?'",
      > "something" , "other");
      > ... or ...
      > MyDataLayer.Exe cuteSQL("SELECT x FROM y WHERE text='?'", "something' OR
      > text='other");
      >
      > P.
      >
      > --
      > www.CL4.org
      >
      >[/color]


      Comment

      Working...