3

In my wiki I have some source code like the snippet posted below.

I have made each MySQL statement in capital letters in bold, by wrapping three apostrophes around it as wikimedia requires.

I now also want to give each statement the color #909 like I did with the first statement.

{| class="wikitable"
|-
!style="width:250px" | MySQL Commands
!style="width:500px" | Example 
!Description
|-
|<span style="color:#909">'''SELECT COUNT'''</span>
|'''SELECT COUNT(*)''' '''FROM''' classics;
|'''SELECT COUNT''' displays the number of rows in the table by passing * as a parameter, which means “all rows.”
|-
|'''SELECT''' and '''SELECT DISTINCT'''
|'''SELECT''' author '''FROM''' classics; <br>'''SELECT DISTINCT''' author '''FROM''' classics;
|'''SELECT DISTINCT''' (or '''DISTINCTROW''') allows you to “weed out” multiple entries when they contain the same data.
|-
|'''DELETE'''
|'''DELETE FROM''' classics '''WHERE''' title='Little Dorrit';
|This example issues a '''DELETE''' command for all rows whose title column contains the string inside the ‘’. 
|-

How can I make a regular expression that finds the sequence of three apostrophes twice, and then place<span style ="color:#909"> before the first group of apostrophes and finally set a </span> after the last group?

Emil Vissing
  • 460
  • 4
  • 19

1 Answers1

1

Try this:

(?<!<span style="color:#909">)('''[A-Z\(\)\*\s]+''')(?!</span>) # find
<span style="color:#909">\1</span> # replace

The two negative lookarounds make it so the regex only matches SQL statements that don't already have the <span> tag. That way you can search and replace as often as you want without ending up with duplicate tags.

This can be done in an editor like Notepad++.

fenceop
  • 1,439
  • 3
  • 18
  • 29
  • Awesome, it works like intended! Quick question: What if I wanted to run the regex again, for when I added more text later on. Woudn't it mess up with the words that have already had the html tags wrapped around them? – Emil Vissing May 09 '15 at 19:37
  • 1
    @EmilVissing I added negative lookarounds to prevent duplicate tags. – fenceop May 10 '15 at 06:10