regex - variable number of match in a regexp -


i have file :

#2/1/1/21=p1 5/1/1/21=p1 isid=104 3/1/1/9=p1 4/1/1/4=p1 5/1/1/17=p1 6/1/1/4=p1 isid=100 1/1/1/4=p1 6/1/1/5=p1 isid=101 

i want line 1 ignored (it commented line) in line 2 want "3/1/1/9" "4/1/1/4" "5/1/1/17" in 3 variables var1 var2 , var3 in line 3 want "1/1/1/4" , "6/1/1/5" in 2 variables var1 , var2.

for moment can ignore line 1, , match want on line 2 or line 3 :

if {[regexp {^(\d/\d/\d/\d{1,2})=p1.*(\d/\d/\d/\d{1,2})=p1} $line value match1 match2]} {       # works line 3 not line 2 }   if {[regexp {^(\d/\d/\d/\d{1,2})=p1.*(\d/\d/\d/\d{1,2})=p1.*(\d/\d/\d/\d{1,2})=p1} $line value match1 match2 match3]} {       # works line 2 not line 3 } 

how can have right number of matched line 2 , 3 ?

try regex:

[regexp {^(\d/\d/\d/\d{1,2})=p1.*?(\d/\d/\d/\d{1,2})=p1(?:.*?(\d/\d/\d/\d{1,2})=p1)?} $line value match1 match2 match3]                                                        ^^^                        ^^ 

this makes third match optional.

i turned greedy .* non-greedy .*?.


to matches, use more this:

if {[string range $line 0 0] ne "#"} {     set matches [regexp -all -inline -- {\d/\d/\d/\d{1,2}(?==p1)} $line] } 

and matches in list $matches. can access them through lindex or if use lassign , give them specific names.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -