×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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 --> TEXT_TO_PARSE

[('xkb', 'us'), ('xkb', 'lt')] 


This is the code that parses the input

CODE --> 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 --> Output

('xkb', 'us')
('xkb', 'lt') 

The output I've got:

CODE --> Output

('xkb', 'us'), ('xkb', 'lt') 

RE: [Regex] Selecting content between round brackets, also including round brackets

Hi

Quote (man bash)

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 --> Bash(fragment)

REGEX_PATTERN="\(([^()]*)\)" 

Feherke.
feherke.github.io

RE: [Regex] Selecting content between round brackets, also including round brackets

(OP)
Thanks, works as expected.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close