Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[Regex] Selecting content between round brackets, also including round brackets 1

Status
Not open for further replies.

boqsc

Technical User
Jan 28, 2020
4
LT
I'm gluing together a small block of bash code that would parse the output of gsettings get command.
I'm not sure what's wrong here.

This is the example of input. (gsettings get org.gnome.desktop.input-sources sources)
Code:
 [('xkb', 'us'), ('xkb', 'lt')]


This is the code that parses the input
Bash:
#!/bin/bash
TEXT_TO_PARSE=$(gsettings get org.gnome.desktop.input-sources sources)
REGEX_PATTERN="\((.*?)\)"
while [[ ${TEXT_TO_PARSE} =~ (${REGEX_PATTERN}) ]]; do
	echo "${BASH_REMATCH[1]}"
	TEXT_TO_PARSE=${TEXT_TO_PARSE##*${BASH_REMATCH[1]}}
done


Expected output:
Code:
('xkb', 'us')
('xkb', 'lt')

The output I've got:
Code:
('xkb', 'us'), ('xkb', 'lt')
 
Hi

man bash said:
An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered a POSIX extended regular expression and matched accordingly (as in regex(3)).
Unfortunately non-greedy quantifiers are PCRE specific, not known by POSIX extended. So you will have to write it as :
Code:
[navy]REGEX_PATTERN[/navy][teal]=[/teal][i][green]"[/green][/i][lime]\([/lime][i][green]([^()]*)[/green][/i][lime]\)[/lime][i][green]"[/green][/i]


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top