perl - Regex for specific file structure -
i need parse file next simple structure:
some string 1 string 2 string 3 string x string y string z ...
file consist of 2 parts separated "\n\n" or "\r\n\r\n". separator present in example after "some string 3". each part optional, if first part omitted there 1(but regex need 2 empty lines) empty line(\n|\r\n) before second part. , if second part omitted there number of empty lines after first part(include no empty lines @ all).
i'm trying achieve desired result regex this:
(?isx: \h* (.+)? \h* (?:(?:\n|\r\n){2,} \h* (.+))? \s*)
but no success because first "(.+)?" greedy , if making 2nd part non-optional violates requirements both part must optional. know can use split /(?:\n|\r\n)/, $str in case file in future have more complex structure can't use split.
can me this?
you might want use non-greedy group, since don't want match seperator.
(?ìsx: (?: (.*?) # non greedy (?:\r?\n){2,} # matches \r\n\n might not of concern |\r?\n) # 1 empty line. (.*) # second group )
i don't know wanted achieve \h
s. if want ensure there in lines (right now, .
match \n
or spaces) try (?:[^\n]+\n)*?
groups.
also, brevities sake, avoided explicit ?
used. there might difference in results. if match nothing under star, you'll empty string, if don't match @ all, value of group-variable undefined
. here short example show difference:
"aa" =~ /(c)?(d*)aa/
here $1
undefined
, while $2
empty string. minor difference might yield annoying warnings or unexpected results if tested defined
contents of group.
Comments
Post a Comment