Grep & Regex

[grep] context

This popular UNIX utility has been adapted to WebDNA. [grep] replaces text based on a regular expression.
[grep search=regexp&replace=regexp] Any Text [/grep]


search(Required) Regular expression that defines what text to search for in the body of the context
replace(Required) Regular expression that defines how to output the resulting text


(Optional) T/F ignores case sensitivity while performing the grep function
Example WebDNA code:
[grep search=([0-9]*-[0-9]*)&replace=<strong>\1</strong>]
  Hi, my phone number is 555-1234, and I'd like you to call me
Hi, my phone number is 555-1234, and I'd like you to call me

Using 'ignorecase' parameter in [grep] context. Search for 'usa' and replace with 'USA'

Example WebDNA code:
[grep search=usa&replace=USA&ignorecase=T]
I was born in the usA
I was born in the uSa
I was born in the Usa
I was born in the USA
I was born in the USA
I was born in the USA

Removing html tags from text

Example WebDNA code:
[grep search=(<[a-z]*>[^<>]*<\/[a-z]*>|<[a-z]*>)&replace=]
  <script>remove the code that's here 1</script> but not here 1 <br> <hr>
  <script>remove the code that's here 2</script> but not here 2 <br> <hr>
  <script>remove the code that's here 3</script> but not here 3 <br> <hr>
but not here 1 but not here 2 but not here 3

Grep is perhaps one of the most powerful and least understood features of WebDNA. The version of grep that is used in WebDNA is a very basic version, similar to UNIX's egrep. Here are a few tips.

Characters to Use in Search Patterns
Character Typed What it Represents Example
any characterrepresents the character typed, with the exception of the special characters defined belowa represents a, b represents b, etc.
. (dot)any character (except line breaks)will match c, 3, space, etc.
#any digitwill match 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9
\dany digitwill match 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 (same as #)
\Dany non digitwill match anything except 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 (anything not matched by \d)
^beginning of line
$end of line
\rline break (return)
\nnewlinematches a Unix line break (line feed)
\fform feed
\swhitespacematches any whitespace character (space, tab, line break, newline, form feed)
\Snon whitespacematches any non whitespace character (anything not matched by \s)
\wword charactermatches any word character. According to the release notes: "A word is any run of non-word-break characters bounded by word breaks. Word characters in the ASCII range are generally alphanumeric, and characters whose value is greater than 127 are also considered word characters." Typically this is letters, numbers, and underscores, but also includes some other characters you might not cosinder word characters.
\Wnon word charactermatches any non word character (anything not matched by \w)
\characterrepresents a character that is normally a special character\\ means \, \. means ., etc.
[any series of characters]represents any of the characters inside the brackets[abc] represents an a, b, or c
[any character-another character]represents any characters within the range of characters specified[a-c] represents a, b, or c
[^any series of characters]represents any character except the ones specified after the ^[^c3] represents any character except c or 3
Special charactersSpecial characters in search patterns that need to be escaped with a \ are: .#^$\?+*|[]()
Inside []'s, you also need to escape -
In replacement patterns, only the following characters need escaping: &\.

[regex] WebDNA v8.1+ context

[regex] is a full implementation of [grep], starting from version 8.1 of WebDNA. The older implementation of [grep] remains for backward compatibility for its simple search and replace capabilities, however, [regex] is a better choice.

Syntax is the same as for [grep], except that it does not have the limitations of WebDNA's implementation of [grep]

Some specific operators used by [regex] will be interpreted by WebDNA, like "%2B" or "%26" for instance. In this case, it is safer to [url][/url] the operator like in the following

Example WebDNA code:
[regex search=[value]&replace=][/regex]
[regex search=[url][value][/url]&replace=][/regex]
Results: both will show