Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5961295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:56:41+00:00 2026-05-22T18:56:41+00:00

I’m quite inexperienced with RegEx – just an occasional straighforward RegEx for a programming

  • 0

I’m quite inexperienced with RegEx – just an occasional straighforward RegEx for a programming task that I worked out by trial and error, but now I have a serious regEx challenge:

I have about 970 text files containing Sybase Transact SQL snippets, and I need to find every table name in those files and preface the table name with ‘ #’. So my options are to either spend a week editing the files by hand or write a script or application using regEx (Python 3 or Delphi-PRCE) that will perform this task.

The rules are as follows:

Table names are ALWAYS upperCase – so I’m only looking for upperCase
words;

Column names, SQL expressions and variables are ALWAYS lowerCase;

SQL keywords, Table aliases and column values CAN BE upperCase, but must NOT be prefixed with ‘ #’;

Table aliases (must not be prefixed) will always have whiteSpace preceding them until the end of the
previous word, which will be a table name.

Column values (must not be prefixed) will either be numerical values or characters enclosed in
quotes.

Here is some sample text requiring application of all the above mentioned rules:

update SYBASE_TABLE
set ok = convert(char(10),MB.limit)
from MOVE_BOOKS MB, PEOPLEPLACES PPL
where MB.move_num = PPL.move_num
AND PPL.mot_ind = 'B'
AND PPL.trade_type_ind = 'P'

So far with I’ve gotten only this far: (not too far…)

(?-i)[[:upper:]]

Any help would be most appreciated.
TIA,

MN

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:56:42+00:00Added an answer on May 22, 2026 at 6:56 pm

    This is not doable with a simple regex-replacement. You will not be able to make a distinction between upper case words that are tables, are string literals or are commented:

    update TABLE set x='NOT_A_TABLE' where y='NOT TABLES EITHER' 
    -- AND NO TABLES HERE AS WELL
    

    EDIT

    You seem to think that determining if a word is inside a string literal or not is easy, then consider SQL like this:

    -- a quote: '
    update TABLE set x=42 where y=666
    -- another quote: '
    

    or

    update TABLE set x='not '' A '''' table' where y=666 
    

    EDIT II

    Okay, I (obsessively) hammered on the fact that a simple regex replacements is not doable. But I didn’t offer a (possible) solution yet. What you could do is create some sort of “hybrid-lexer” based on a couple of different regex-es. What you do is scan through the input file and at the start of each character, try to match either a comment, a string literal, a keyword, or a capitalized word. And if none of these 4 previous patterns matched, then just consume a single character and repeat the process.

    A little demo in Python:

    #!/usr/bin/env python
    import re 
    
    input = """
    UPDATE SYBASE_TABLE
    SET ok = convert(char(10),MB.limit) -- ignore me!
    from MOVE_BOOKS MB, PEOPLEPLACES PPL
    where MB.move_num = PPL.move_num
    -- comment '
    AND PPL.mot_ind = 'B '' X'
    -- another comment '
    AND PPL.trade_type_ind = 'P -- not a comment'
    """
    
    regex = r"""(?xs)          # x = enable inline comments, s = enable DOT-ALL
      (--[^\r\n]*)             # [1] comments
      |                        # OR
      ('(?:''|[^\r\n'])*')     # [2] string literal
      |                        # OR
      (\b(?:AND|UPDATE|SET)\b) # [3] keywords
      |                        # OR
      ([A-Z][A-Z_]*)           # [4] capitalized word
      |                        # OR
      .                        # [5] fall through: matches any char
    """
    
    output = ''
    
    for m in re.finditer(regex, input): 
        # append a `#` if group(4) matched
        if m.group(4): output += '#'
        # append the matched text (any of the groups!)
        output +=  m.group()
    
    # print the adjusted SQL
    print output
    

    which produces:

    UPDATE #SYBASE_TABLE
    SET ok = convert(char(10),#MB.limit) -- ignore me!
    from #MOVE_BOOKS #MB, #PEOPLEPLACES #PPL
    where #MB.move_num = #PPL.move_num
    -- comment '
    AND #PPL.mot_ind = 'B '' X'
    -- another comment '
    AND #PPL.trade_type_ind = 'P -- not a comment'
    

    This may not be the exact output you want, but I’m hoping the script is simple enought for you to adjust to your needs.

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.