regex - Capturing group not working at end of -Pattern for Select-String -


i've started working regex in powershell , have come across unexpected response select-string cmdlet.

if enter following:

$thing = "135" | select-string -pattern "(.*?)5" $thing.matches 

you receive expected result match-info object:

groups   : {135, 13} success  : true captures : {135} index    : 0 length   : 3 value    : 135 

but if place capturing group @ end of -pattern:

$thing = "135" | select-string -pattern "(.*?)" $thing.matches 

the match-info doesn't seem find anything, although 1 created:

groups   : {, } success  : true captures : {} index    : 0 length   : 0 value    :  

as said, i'm quite new powershell, expect behavior operator error.

but work around? behavior hasn't caused me problems yet, considering files i'm working (electronic manuals contained in xml files), expect eventually.

...

with regards,

schwert

...

clarification:

i made example simple illustrate behavior, original issue pattern:

$linkname = $line | select-string -pattern "`"na`"><!--(?<linkname>.*?)" 

the file 1 of our indices links between manuals, , name of link contained within comment block located on each line of file.

the pattern typo, name , comment don't go way end of line. found when program began giving errors when couldn't find "linkname" in match-info object.

once gave characters occur after link name (::), worked correctly. putting example:

$linkname = $line | select-string -pattern "`"na`"><!--(?<linkname>.*?)::" 

i'm no regex expert believe pattern "(.*?)" problem. if remove ?, example, groups expected.

also, please don't use regex parse xml. :) there's easier ways such as:

[xml]$manual = get-content -path c:\manual.xml 

or

$xdoc = new-object system.xml.xmldocument $file = resolve-path c:\manual.xml $xdoc.load($file) 

once you've got in structured format can use dot notation or xpath navigate nodes , attributes.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -