Hi how would I go about removing spaces from strings such as the following: 1 2 B L GROW A I M INC becomes 12 BL GROW AIM INC
-
Login or Register
- Log in with
s/ (\d) (\d) / \1\2 /g
s/ (\d) (\d) / \1\2 /g
version 14 clear input str30(stringvar) "1 2 B L GROW A I M INC" "W H O CREATED T H I S MESS" "R E G E X ROCK" end replace stringvar=ustrregexra(stringvar,"(?<![A-Z0-9][A-Z0-9])(?=[ ][A-Z0-9]( |$)) ","",0) list
http://userguide.icu-project.org/strings/regexpWhile writing this response, I discovered that whatever I did the other day that convinced me at that time that ustrregexra() did not support back references in the substitution string was incorrect. I would not need Perl to do what I hoped, leveraging my ancient understanding of regex syntax.
. clear . set obs 1 number of observations (_N) was 0, now 1 . generate str20 text = "A 1 2 B 3 4 C" . generate str20 new = ustrregexra(text, " (\d) (\d) "," \1\2 ") . list, noobs +-----------------------------+ | text new | |-----------------------------| | A 1 2 B 3 4 C A 12 B 12 C | +-----------------------------+
clear
input str39 stringvar
"1 2 B L GROW A I M INC"
"3 4 1 B L D G NUMBER LETTER SEPARATE"
"R O O M 1 4 5 KINGCROSS R O A D"
"SUCH A BEAUTIFUL DAY"
"OOPS, THIS I S A W R O N G ONE"
end
replace stringvar = trim(itrim(stringvar))
split stringvar, gen(v)
forval i = `r(nvars)'(-1)2 {
replace v`i' = " "+ v`i' if length(v`i')*length(v`=`i'-1')>1 | 0*real(v`i') != 0*real(v`=`i'-1')
}
egen new_stringvar = concat(v*)
drop v*
gen companyname2 = subinstr(companynamecrsp, " ", ".",.) split companyname2, parse(.) gen(companies) gen companyappend1=companies1 local i=2 local j=1 local k=3 foreach comps of varlist companies* { replace companyappend1=companyappend1+" "+companies`i' if strlen(companies`i')>1 & companies`i'!="&" replace companyappend1=companyappend1+companies`i' if strlen(companies`i')==1 & companies`i'!="&" replace companyappend1=companyappend1+companies`i' if strlen(companies`j')==1 & strlen(companies`k')==1 & companies`i'=="&" replace companyappend1=companyappend1+" "+companies`i' if strlen(companies`j')>1 & strlen(companies`k')>1 & companies`i'=="&" replace companyappend1=companyappend1+" "+companies`i' if strlen(companies`j')>1 & strlen(companies`k')==1 & companies`i'=="&" replace companyappend1=companyappend1+" "+companies`i' if strlen(companies`j')==1 & strlen(companies`k')>1 & companies`i'=="&" local i=`i'+1 } drop companyname31 companies*
. clear . set obs 1 number of observations (_N) was 0, now 1 . generate str20 text = "A 1 2 B 3 4 C" . generate str20 new = ustrregexra(text, " (\d) (\d) "," $1$2 ") . list, noobs +-----------------------------+ | text new | |-----------------------------| | A 1 2 B 3 4 C A 12 B 34 C | +-----------------------------+
Comment