DATA Step, Macro, Functions and more

Enhanced Editor and Replace with Regular Expressions

Reply
Frequent Contributor
Posts: 91

Enhanced Editor and Replace with Regular Expressions

I know that I can use regular expressions in the Enhanced Editor to find strings. Is it possible to use regular expressions in the "Replace with:" box on the Replace window?

Thank you.
Respected Advisor
Posts: 3,887

Re: Enhanced Editor and Replace with Regular Expressions

I can understand how a regular expression works for finding patterns - but how would a regular expression work for replacing a string? How exactly would it replace it??
Frequent Contributor
Posts: 91

Re: Enhanced Editor and Replace with Regular Expressions

Unix tools like SED and AWK, Perl, too, I think, have provisions for remembering the "found" string and then altering it, like changing lower case to upper case, leaving all numerics alone and deleting the characters, etc.

Probably a little to specialized for an IDE editor.
PROC Star
Posts: 1,555

Re: Enhanced Editor and Replace with Regular Expressions

It is possible.
For example, enter \d in the find text box and enter 4 in the replace with box to replace all digits with 4.
There seems to be a very limited set of regular expressions that can be used however.
Frequent Contributor
Posts: 91

Re: Enhanced Editor and Replace with Regular Expressions

I was hoping that I might be able to do something like
find string is \a\d\a
replace string is \a_\a
to replace all strings that have an alpha followed by a digit followed by and alpha to be replaced by the original alpha, and underscore, and the original trailing alpha.

I think I am asking for a little too much. There are other editors out there that will allow me to do something like this, or I can handle it in other ways.

Thanks for your thoughts.

This thread can be closed
Super Contributor
Posts: 394

Re: Enhanced Editor and Replace with Regular Expressions

Many text editors I've used, and many programming languages, support replacement by regular expressions. The simplest case is replacing the text that is matched with new text. For example (in vi)

s/June/July/

replaces the text "June" with "July". You can also identify a part or several parts of the regular expression (typically by surrounding it/them with parentheses). The text matched by that part is replaced with new text. For example, in my text editor I can specify this regular expression as a search target:

(\c+)blank

This means "any string that starts with at least one alphabetic character followed by "blank". The parentheses around \c+ "tags" that part of the match. Then I specify this as the replacement string:

blank\1

This replaces the match with the string blank followed by the first (in our case, only) tagged expression. That is, whatever \c+ matched.

So, if I have the words "ageblank" and "sexblank" in my text, they are replaced by "blankage" and "blanksex", respectively.
PROC Star
Posts: 1,555

Re: Enhanced Editor and Replace with Regular Expressions

It would make sense that the enhanced editor could do what you both mention.

Indeed, the fact that a regexp check box is available would seem to imply that this is the case.

The only use I have found for this feature is to search (not replace) using the few expressions available using the > button beside the find box.
Ask a Question
Discussion stats
  • 6 replies
  • 407 views
  • 0 likes
  • 4 in conversation