regex - Bash shell(grep) equivalent of this python regular expression? -
i have written regular expression match hyphenated word in python
regexp = r"[a-z]+(?:-[a-z]+)*" it matches words 0 or more hyphens. e.g. abc,acd-def,x-y-y etc. however, can't find grouping operator ?: shell(for instance using grep). seems me feature of python regex not standard regex.
can please tell me how write same regex in shell?
(?:pattern) matches pattern without capturing contents of match. used following * allow specify 0 or more matches of contents of ( ) without creating capture group. affects result in python if used re.search(), matchobject not contain part (?: ). in grep, result isn't return in same way, can remove ?: use normal group:
grep -e '[a-z]+(-[a-z]+)*' file here i'm using -e switch enable extended regular expression support. output each line matching pattern - can add -o switch print matching parts.
as mentioned in comments (thanks), is possible use back-references (like \1) grep refer previous capture groups inside pattern, technically behaviour being changed removing ?:, although isn't you're doing @ moment doesn't matter.
Comments
Post a Comment