BookmarkSubscribeRSS Feed
advoss
Quartz | Level 8
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.
6 REPLIES 6
Patrick
Opal | Level 21
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??
advoss
Quartz | Level 8
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.
ChrisNZ
Tourmaline | Level 20
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.
advoss
Quartz | Level 8
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
Tim_SAS
Barite | Level 11
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.
ChrisNZ
Tourmaline | Level 20
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1354 views
  • 0 likes
  • 4 in conversation