WEEK NINE

After trying multiple options for getting the right regexes for our filter, I finally got the correct regular expressions for concatenating an infinite number of strings. An equal regex was written to match the TOP N pattern of SQL Server syntax.
For string concatenation, I wrote and tested the following regular expression.

"[^"]*"(?: *\+ *"[^"]*")+

The above regex matches string concatenation and can be tested online HERE.
Now lets explain the regex:

"[^"]*"(?: *\+ *"[^"]*")+/g
  •  matches the characters  literally
  • [^"]* match a single character not present in the list below
    • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    •  a single character in the list  literally (case sensitive)
  •  matches the characters  literally
  • (?: *\+ *"[^"]*")+ Non-capturing group
    • Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    •  * matches the character   literally
      • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    • \+ matches the character + literally
    • * matches the character   literally
      • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    •  matches the characters  literally
    • [^"]* match a single character not present in the list below
      • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
      •  a single character in the list  literally (case sensitive)
    •  matches the characters  literally
  • g modifier: global. All matches (don’t return on first match)

For the TOP N regex, I wrote a simple regex
“top”\d

Now We will improve our program to use these new regular expressions to match and rewrite the queries accordingly.

For string concatenation, we save the matched string and substitute all plus (+) with commas (,). Then  next we wrap this new string in CONCAT(new_string).  This is further substituted in the original query to have our new syntax for string concatenation.

For the TOP N syntax a simple algorithm is match the pattern and save it in a variable. replace the matched position with a white space and substitute TOP with LIMIT. Our new string is appended to the end of the new query to get our new results filtering syntax.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s