1

我需要将文本中的空格更改为下划线,但只有单词之间的空格,而不是数字之间的空格,例如

"The quick brown fox 99 07 3475"

会成为

"The_quick_brown_fox 99 07 3475"

我尝试在数据步骤中使用它:

mytext = prxchange('s/\w\s\w/_/',-1,mytext);

但结果不是我想要的

"Th_uic_row_ox 99 07 3475"

关于我能做什么的任何想法?

提前致谢。

4

3 回答 3

7
Data One ;
X = "The quick brown fox 99 07 3475" ;
Y = PrxChange( 's/(?<=[a-z])\s+(?=[a-z])/_/i' , -1 , X ) ;
Put X= Y= ;
Run ;
于 2012-08-24T15:30:58.597 回答
3

当您想将“W W”更改为“W_W”时,您正在将“W W”更改为“_”

所以 prxchange('s/(\w)\s(\w)/$1_$2/',-1,mytext);

完整示例:

 data test;
mytext='The quick brown fox 99 07 3475';
newtext = prxchange('s/([A-Za-z])\s([A-Za-z])/$1_$2/',-1,mytext);
put _all_;
run;
于 2012-08-24T15:18:58.123 回答
1

您可以使用 CALL PRXNEXT 函数查找每个匹配项的位置,然后使用 SUBSTR 函数将空格替换为下划线。我已将您的正则表达式更改为 \w 匹配任何字母数字字符,因此它应该在数字之间包含空格。我不确定您是如何使用该表达式获得结果的。无论如何,下面的代码应该给你你想要的。

data have;
mytext='The quick brown fox 99 07 3475';
_re=prxparse('/[a-z]\s[a-z]/i'); /* match a letter followed by a space followed by a letter, ignore case */
_start=1 /* starting position for search */;
call prxnext(_re,_start,-1,mytext,_position,_length); /* find position of 1st match */
    do while(_position>0); /* loop through all matches */
        substr(mytext,_position+1,1)='_'; /* replace ' ' with '_' for matches */
        _start=_start-2; /* prevents the next start position jumping 3 ahead (the length of the regex search string) */
        call prxnext(_re,_start,-1,mytext,_position,_length); /* find position of next match */ 
end;
drop _: ;
run;
于 2012-08-24T15:17:17.627 回答