如何提取,计算和返回凌乱文本中的数字?

时间:2019-04-09 09:23:41

标签: regex sas

这是一个更快的问题。
有一个混乱的文本,它是表名称的定义,我想要的就是获取每个表号加15。

我认为perl正则表达式或左值substr()函数可能会有所帮助,但是我不太熟练解决它。

我将给出以下示例:

data test;
    input x $300. @@;
    datalines4;
    %let Tit_Tab5_15    =%NRSTR(Tab5-15 Cross-tabulation of blood routine results(SS) );
    %let Tit_Tab5_16    =%NRSTR(Tab5-16 Cross-tabulation of urine routine results(SS) );
    %let Tit_Tab5_17    =%NRSTR(Tab5-17 Cross-tabulation of blood chemistry results(SS) );
    %let Tit_Tab5_18    =%NRSTR(Tab5-18 Cross-tabulation of electrolyte results(SS) );
    %let Tit_Tab5_19    =%NRSTR(Tab5-19 Cross-tabulation of coagulation results(SS) );
    %let Tit_Tab5_20    =%NRSTR(Tab5-20 Cross-tabulation of blood lipid results(SS) );
;;;;
run;

输出应为:

%let Tit_Tab5_30    =%NRSTR(Tab5-30 Cross-tabulation of blood routine results(SS) );
%let Tit_Tab5_31    =%NRSTR(Tab5-31 Cross-tabulation of urine routine results(SS) );
%let Tit_Tab5_32    =%NRSTR(Tab5-32 Cross-tabulation of blood chemistry results(SS) );
%let Tit_Tab5_33    =%NRSTR(Tab5-33 Cross-tabulation of electrolyte results(SS) );
%let Tit_Tab5_34    =%NRSTR(Tab5-34 Cross-tabulation of coagulation results(SS) );
%let Tit_Tab5_35    =%NRSTR(Tab5-35 Cross-tabulation of blood lipid results(SS) );

请注意,部分的编号(单词Tab之后的编号)不应更改。

也欢迎不使用prx系列功能的解决方案。

2 个答案:

答案 0 :(得分:1)

您可能会从中得到一些想法。

(%.*?-)(\d+)(.*;)



 Match 1
1.  %let Tit_Tab5_15 =%NRSTR(Tab5-
2.  15
3.  Cross-tabulation of blood routine results(SS) );
Match 2
1.  %let Tit_Tab5_16 =%NRSTR(Tab5-
2.  16
3.  Cross-tabulation of urine routine results(SS) );
Match 3
1.  %let Tit_Tab5_17 =%NRSTR(Tab5-
2.  17
3.  Cross-tabulation of blood chemistry results(SS) );
Match 4
1.  %let Tit_Tab5_18 =%NRSTR(Tab5-
2.  18
3.  Cross-tabulation of electrolyte results(SS) );
Match 5
1.  %let Tit_Tab5_19 =%NRSTR(Tab5-
2.  19
3.  Cross-tabulation of coagulation results(SS) );
Match 6
1.  %let Tit_Tab5_20 =%NRSTR(Tab5-
2.  20
3.  Cross-tabulation of blood lipid results(SS) );

在每次比赛中获取第2组值,然后添加“ 15”,然后替换它。

https://rubular.com/r/HPQAetIFcfGOHD

答案 1 :(得分:1)

使用prxchange和tranwrd。

typedef struct Foo
{
    int a;
} Foo;

typedef void (*destructor)(const Foo* p, const void* context);


void destroyFoo1(const Foo* p, const void* context);
void destroyFoo1(const Foo* p, const void* context) 
{
   free((void*)p);
   if (*((int*)context) == 0) {
       printf("hello world\n");
   }
}
void destroyFoo2(const Foo* p);
void destroyFoo2(const Foo* p) 
{
    free((void*)p);
}
int main(void)
{
    //this is okay
    destructor destructor1 =  destroyFoo1;
    //this triggers a warning
    destructor destructor2 = destroyFoo2;
    //This doesn't generate a warning
    destructor destructor3 = (destructor)destroyFoo2;

}