perl - how to fetch inner values from Regex nested backreference -


i receive input server in following manner (sample input data):

[1284336000]: host1;event1;flag;state;counter;errors or warnings [1284336000]: host2;event1;flag;state;counter;errors or warnings [1284336000]: host1;event2;flag;state;counter;errors or warnings [1284336000]: host2;event2;flag;state;counter;errors or warnings 

i have match input , based on match, create variable value hostname-eventname.

my regex

^\[\d+\]:\s((host1);(event1)|(host2);(event2)|(host3);(event2)|(host2);(event1));(\w+);(\w+);(\d).+$ 

i want name of host , event separately in reference variables $2 or $3.

for example, consider input:

[1284336000]: host1;event1;flag;state;counter;errors or warnings 

i need create variable name <hostname-eventname> according hostname , eventname fetched match above.

say,

$myvar=$2-$3  (that is, $myvar=host1-event1) 

i cannot apply split operation further. no programming: can read input data. , yes, regex of perl regex type.

i don't know if clarified query or not?

you need use branch-reset operator, (?|…|…|…):

^\[\d+\]:\s(?|(host1);(event1)|(host2);(event2)|(host3);(event2)|(host2);(event1));(\w+);(\w+);(\d).+$ 

or more legibly:

m{     ^ \[ \d+ \] : \s     (?| (host1);(event1)  # $1, $2       | (host2);(event2)  # $1, $2       | (host3);(event2)  # $1, $2       | (host2);(event1)  # $1, $2     )     ;     (\w+);(\w+)           # $3, $4     ; (\d)                # $5     .+ $ }x 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -