REGEX - anthonyblackham/GIS-Wiki GitHub Wiki

Regex is short for Regular Expressions, it's a nice set of tools to manipulate text, it's basically find and replace on steroids.

Notepad ++ integrates regular expressions.

Lookahead/Lookbehind

Lookaheads and lookbehinds let you look ahead or behind a character or word. There are both positive and negative versions, eg look ahead and see if the character is a space (Positive) or look ahead and see if the character is not a space (Negative).

Syntax:

  • Lookahead(Positive): (?=insertsearchtermhere)
  • Lookahead(Negative): (?!insertsearchtermhere)
  • Lookbehind(Positive): (?<=insertsearchtermhere)
  • Lookbehind(Negative): (?<!insertsearchtermhere)

Advanced examples:

Lets say I have some rows that use # as a delimiter but also use # in the text:

Look at Document #3#Look at Document #3

I want to delete the first half before the delimiter.

(.*)(?<!\s)(?!.\s)#

(.*) = Select all characters

(?<!\s) = Check that the character before # is not a space

(?!.\s) = Check that the character after # is not a space

# = Delimiter I'm searching for

Replacing the resulting selected text with nothing results in:

Look at Document #3

Another example:

Lets say I want to only delete 0's that are at the beginning of the line I'd do:

(?=^)0