Regex 使用“prxchange”更改空格,但不是所有空格

Regex 使用“prxchange”更改空格,但不是所有空格,regex,sas,Regex,Sas,我需要将文本中的空格改为下划线,但仅限于单词之间的空格,而不是数字之间的空格,例如 "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" 我能做些什么 提前感谢。您可以使用CALL PRXNEXT函数查找每个匹

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

"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"
我能做些什么


提前感谢。

您可以使用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;

您可以使用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;
你正在改变 W W 到 _ 当你想改变的时候 W W 到 W_W

所以 prxchange的/\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;
你正在改变 W W 到 _ 当你想改变的时候 W W 到 W_W

所以 prxchange的/\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;

是的,这比我的解决方案简单一点。我必须学习PERL更精细的细节。是的,这比我的解决方案要简单一点。我必须学习PERL的更多细节。