regex - regular expressions in perl explanation -


I need help understanding the regular expressions given below.

Can someone say what it means M / ^ (\ d + \ / \ d + \ / \ d + \ s \ d + \: \ d + \: \ D + \. \ D +) / MSX

  foreach my $ line ({{$ self-> {'stdout'}}) {if ($ line = ~ M / ^ (\ d + \ / \ d + \ / \ d + \ s \ d + \: \ d + \: \ d + \. \ D +) / msx) {$ timestamp = $ 1;  

this

  if ($ line = ~ M / ^ (\ d + \ / \ d + \ / \ d + \ s \ d + \: \ d + \: \ d + \. \ D +) / msx) {  

It is quite unreadable, however, the author started making it readable because it contains the / x flag (white space allowed, but it is not used), but it is still backslashtis Does it really mean that it suffers from and does not limit the matches.

Rewrite it with different delimiters, allowing you to get rid of some backslashes:

  if ($ line = ~ m {^ (\ d + \ D + / \ d + \ s \ d +: \ d +: \ d + \. \ D +)} msx {  

adding white space and [.] Using to match a single point and adding a comment can provide a better idea of ​​what will be matched:

  if ($ line = ~ m {^ # (Beginning of line) (# (Capture group $ 1) \ D + / \ d + / \ d + # digits (squash points) Slash A A (s) \ s # Any white space character (space, tab, etc.) \ d +: \ D +: \ d + # digits (box number) colon numeral (.) [.] \ D + # point digit (Numeral) # (end capture group $ 1)} MSX), where  digit (s)  means an ore plus digits 0-9 (or Any utf8 points). So it will happily match some "00000/0000/0000000000 0000: 000000000000000000000000000000.0000", but it seems that to match them means "0000/00/00 00: 00: 00.000" (milliseconds Including one time ticket). 

A better regex (this is a lesser chance of not matching anything, although it is anchor to start

  if ($ line = ~ m {^ ^ }), Then something like this would happen: the line does not have any real practical difference, but as a general rule this is specific to you) ([0- 9] {4} / [0- 9 ] {2} / [0-9] {2} [] # space character [0- 9] {2}: [0- 9] {2}: [0- 9] {2} [.] [0- 9] {3})} MSX {hand-in-hand, EGG connected to other people should already understand more . 


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -