Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: subinstr and special characters ? and *


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: subinstr and special characters ? and *
Date   Sat, 14 Aug 2010 18:10:07 +0000

<>

As Nick suggests, use use a regex function:
*************!
cap macro drop _all

//example 1//
clear all
local a p_2_3456
 local b1  = regexr("`a'" , "_[0-9]+_" ,"__")
 local b2  `=regexr("`a'" , "_[0-9]+_" ,"__")'
 local b3  "`=regexr("`a'" , "_[0-9]+_" ,"__")'"
macro dir _b1 _b2 _b3


//example 2:   if instead "`a'" is a variable you would:
clear all
set obs 1
local a p_2_3456
g i = "`a'"
g i2 =  regexr("`a'" , "_[0-9]+_" ,"__")
li
local b = i2
di "`b'"


//example 3//
	/*
finally, the macro extended_fcn commands don't have regex, just subinstr
so, you could use a loop  & subinstr to cycle over the possible characters 
you want to  remove if your set on using extended_fcns 
	*/

clear all
	numlist  "1(1)9"
foreach n in `c(alpha)'  `c(alpha)'  `r(numlist)'  {
 	qui local a2: subinstr local a "_`n'_" "__", count(local break)
		if `break'>0 {
			qui local b= "`a2'"
		}
	}
di "`b'"
*************!

~ Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754



On Aug 13, 2010, at 5:25 AM, A Loumiotis wrote:

> Hi,
> 
> Is there a way to use subinstr to replace a pattern that is given with
> the special characters ? or * with something else?
> 
> For example:
> 
> local a p_2_3456
> local b: subinstr local a "_?_" "__"
> di "`b'"
> local b=subinstr("`a'","_?_","__",1)
> di "`b'"
> 
> I would expect that b will now be p__3456 but subinstr does not
> recognize _?_ to include _2_ .
> Is there anyway to do this?
> Thanks,
> Antonis




*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index