vbscript - String between a line -


by google resources write program extract string between 2 specific strings. not print value or store value variable using regular expressions. here code.

prgname = instr(vtext, "program") sub_prgname = mid(vtext, prgname, 100) msgbox sub_prgname, vbinformation dim regex: set regex = new regexp regex.ignorecase = true regex.pattern = "program(.*)?variant" set regex = regex.execute(prgname) msgbox regex.value, vbinformation 

i want string b/w program , variant. when try see output says

run time error 438, object doesn't support property.

this value want parse using regex:

program  sqlplus $sops_mipo_user/$sops_mipo_password@$db_sid @mipo_pruning.sql  variant 

when in doubt, read documentation. execute method returns matches collection, need iterate on collection desired result (in case first submatch).

for each m in regex.execute(prgname)   msgbox m.submatches(0), vbinformation next 

demonstration:

>>> s = "program sqlplus $sops_mipo_user/$sops_mipo_password@$db_sid @mipo_pruning.sql variant n/a" >>> set re = new regexp >>> re.pattern = "program(.*)?variant" >>> re.ignorecase = true >>> for each m in re.execute(s) : wscript.echo m.submatches(0) : next  sqlplus $sops_mipo_user/$sops_mipo_password@$db_sid @mipo_pruning.sql

theoretically without loop:

set m = regex.execute(prgname)(0) msgbox m.submatches(0), vbinformation 

however, regex.execute(prgname)(0) raise error if regular expression didn't find match, evaluating results in loop safer approach.

i'd remove ? in regular expression, though, because it'll make group optional, you're not guaranteed have submatches(0) item. use program(.*)variant instead. if there's no text between "program" , "variant" you'll zero-length string first submatch. or put inside parentheses right after asterisk (program(.*?)variant) make match non-greedy (shortest match instead of longest match).

if input string contains newlines need use [\s\s] instead of ., because dot in regular expressions matches character except newlines.

demonstration:

>>> s = "program" & vbnewline & vbnewline _ & "sqlplus $sops_mipo_user/$sops_mipo_password@$db_sid @mipo_pruning.sql" _ & vbnewline & vbnewline & "variant" >>> wscript.echo s program  sqlplus $sops_mipo_user/$sops_mipo_password@$db_sid @mipo_pruning.sql  variant >>> set re = new regexp >>> re.pattern = "program(.*)?variant" >>> re.ignorecase = true >>> for each m in re.execute(s) : wscript.echo m.submatches(0) : next >>> re.pattern = "program([\s\s]*)?variant" >>> for each m in re.execute(s) : wscript.echo m.submatches(0) : next   sqlplus $sops_mipo_user/$sops_mipo_password@$db_sid @mipo_pruning.sql   

as side note, shouldn't replace regular expression object result of execute method.

set regex = regex.execute(prgname)    '<-- never this! 

re-using variables no-no, don't it.


Comments