Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Cleaning Strings and Regular Expressions


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Cleaning Strings and Regular Expressions
Date   Thu, 24 Nov 2011 09:54:01 +0000

The alternative would be

gen new_x = x
foreach c in . , ; {
          replace new_x = subinstr(new_x, "`c'", "", .)
}

and I don't see what is so bad about that.

Nick

On Thu, Nov 24, 2011 at 12:45 AM, Tirthankar Chakravarty
<tirthankar.chakravarty@gmail.com> wrote:

> Well, if it helps you, the -strip- package (SSC, Paul Seed) automates
> the required -subinstr()- in a -for- loop.
>
> clear*
> set obs 1
> g x = "hi.there,gu;ys."
> strip x, of(".,;") g(new_x)
> li


On Wed, Nov 23, 2011 at 2:57 PM, Cory Smith <corybsmith@gmail.com> wrote:

>> I'm looking to use regular expressions or another command to remove an
>> arbitrary list of characters from a string. For example, I might want
>> to remove all punctuation so
>> hi.there,gu;ys. becomes hithereguys
>>
>> I'd love to use regexr, but it only replaces the first substring. Is
>> there a way to use (Stata) regular expressions for this? Otherwise I'm
>> stuck with running subinstr in a for loop.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index