SAS:Tables of Perl Regular Expression (PRX) Metacharacters:prxparse,prxmatch

SS Data;
SET dd;
IF _N_ the then RET = PRXPARSE. 1 = ( "/ ABC /");
The retain RET;
position = PRXMATCH (RET, String);
RUN;
PRXPARSE prx only be used with other functions. Position returns the position in string "abc" is. Fixed usage.
(Find find a character determined, prxparse series can be used to find matching character, or is a template string)
1, PRXPARSE ( "/ \ d \ d \ d /") to find any three numbers to note here the keyword must be { "//"} circled.
Control character will be added in front of \, no need to add the specific character \, the string to be searched with "/ /" enclosed.
2, Prxparse ( "/ \ d + /") to find more than one number that can be 1 100 may be
a digital, if a plurality of short, use the "+."
3, Prxparse ( "/ \ w \ w \ w * /") and two or more two Looking then separated by a space character, an asterisk followed by a space behind. For example looking la la la la la, or you can write Prxparse ( "/ \ la \ la \ la * /") *
indicate that two and two above, * represents the space after the found string should include back a space

4, Prxparse ( "/ \ w \ w? + /") To find one or two characters separated by a space or more spaces. ? Followed by a space, ha. This sum up, is a character with many asterisk, spaces, and numbers more with the + sign.
? It means one or two characters? Indicates a space behind the character string found should include trailing spaces, represents a plurality of spaces +
5, Prxparse ( "/ (\ w \ w) + (\ d) + /") between the two characters to find and a number of characters separated by one or more spaces. Note that the regular expression, the space can be considered, so be careful when you write. For those here chestnuts, is such as "I'm La La 2" then you will find: "Lala 2."
6, *: cat * matches " cat", "cats", "catanddog"; c (at) * matches "c", "cat", "catatat";
look * preceding subexpression zero or more times .
+: + Find preceding subexpression one or more times.
?: Find preceding subexpression? Zero or one. know? Matches "kno" and " know".
: On behalf of a specific character, such as rn matches "run" or "ran ".
\ d represents any of a number of 0-9
\ D representative of any non-0-9
^ represents a beginning of the string, for example, CAT The matches ^ "CAT" or "CATS" But Not "The CAT"
$ represents a character the end of the string, such as cat $ matches "the cat" but not "cat in the hat"
[XYZ] behalf of any character within square brackets, e.g. CA [RT] The matches "CAT" or "CAR"
[the Z-zA-A], [AZ] Ibid
{n} n times repeating the previous sub-strings, e.g. \ d . 5} = {\ D \ D \ D \ D \ D
{n.} repeat the previous substring n or more times
{n, m} repeat the previous substring to n times m times
[^ abcxyz] In addition to matching any character other than abcxyz, e.g. [^. 8] \ D \ D the matches But Not 123 812
x | x Y matches or y, for example, C (a | O) T the matches "CAT" and "COT"
\ S represents any whitespace
\ S on behalf of any character except space
\ w represent any character, regardless of capitalization, spaces, underscores
\ (on behalf of (
\) on behalf of)
/ ...... / i is not case sensitive

%macro date_covert(var);
 &var.=tranwrd(&var.,'/','-');
 * Compile the Perl Regular Expression;
 retain pattern;
 if _N_=1 then
pattern=prxparse("/(\d{4}|-|UN)?-?(\d{2}|\d{1}|-|UN)?-?(\d{2}|\d{1}|-|UN)?T?(\d{2}|-)?:?(\d{2}|-)?:?(\d{2}|-)?/");

 * get each part of the datetime variable;
 match=prxmatch(pattern,&var.);
 yy=prxposn(pattern,1,&var.);
 mm=prxposn(pattern,2,&var.);
 dd=prxposn(pattern,3,&var.);

 YY pattern match dd mm drop;
% MEND;
PRXPARSE execution is not 0, then the execution prxmatch is not 0, and then finally perform prxposn, 1,2,3 template bracketed by first, second and third section.

% Macro dtderive (datein =, dateout =);
 & datein = Strip (compbl (& datein).);.
 The retain RET;
 IF _N_ = 1 the then RET = PRXPARSE ( "/ (\ d \ d | \ w \ w)?? ? \ S + (\ D \ D \ D) \ S (\ D {. 4} | \ W {. 4}) / I ");??
 match = PRXMATCH (RET, & datein);.
 YY = prxposn (RET,. 3 , & datein);.
 mm = prxposn (RET, 2, & datein);.
 dd = prxposn (RET,. 1, & datein);.
 YY1 = tranwrd (YY, "0000", "");
 MM1 = PUT (the upcase (Strip (mm)), $ Mon);.
 dd1 = tranwrd (tranwrd (lowcase (dd), "UN", ""), "UK", "");
* If prxchange, followed by calculation into dd1 time, yet Work; prxchange ( 'S / U | n-| K / I /', -. 1, dd)
* date deletion format into iso8601 form;
 IF DD1 = "" and MM1 = "" the then & dateout = YY1;.
 else if dd1 = "" then & dateout.=cats(coalescec(yy1,"-"),"-",coalescec(mm1,"-"));
 else if dd1 ne "" then &dateout.=cats(coalescec(yy1,"-"),"-",coalescec(mm1,"-"),"-",put(input(dd1,??best.),z2.));
%mend;

Guess you like

Origin www.cnblogs.com/jwcutey/p/12159361.html