I have a dataset that contains a number of closed brackets with data. I don't want the data which exists in the brackets. How can I remove that in QGIS?
Mcdonalds(Park Avenue)
KFC (Park Avenue) Restaurant
(Financial District) Hotel Marriot
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityUse this expression, where name
is the name of the input attribute field:
regexp_replace ("name", '\\(.+\\)','')
If you also want to avoid any whitspaces (space etc.) at the beginning or end of the text field, e.g. resulting from deleting a bracket at the beginning (like your 3rd example), add a trim()
function:
trim (regexp_replace ("name", '\\(.+\\)',''))
The challenge with removing what is between the parentheses with regex_replace
is that one can end up with multiple spaces between the two parts, e.g., "KFC⎵(Park⎵Avenue)⎵Restaurant" getting replaced with "KFC⎵⎵Restaurant". Whereas trim
addresses additional spaces at the start and end of the string, it doesn't address additional spaces in the middle of the string.
Sticking with regex_replace
and removing the parenthetical content, one can replace the parenthetical content with an empty string and then split the string apart and re-assemble it to remove extra spaces. array_filter
is needed so that spaces and empty strings from splitting do not get re-inserted as spaces into the new string.
array_to_string(
array_filter(
string_to_array(
regexp_replace("name", '\\(.*\\)',''),
' '
),
@element
),
' '
)
Using RegExr: Learn, Build, & Test RegEx to explain this regular expression:
\( Escaped character. Matches a "(" character (char code 40).
. Dot. Matches any character except line breaks.
* Quantifier. Match 0 or more of the preceding token.
\) Escaped character. Matches a ")" character (char code 41).
As opposed to removing what is between the parentheses, one can take the approach of identifying what exists before and after the parentheses using regex_matches
and building a new string from those elements while ignoring what is between the parentheses.
array_to_string(
array_filter(
regexp_matches(
"name",
'^\\s*(.*?)\\s*\\(.*\\)\\s*(.*?)\\s*$'
),
@element
),
' '
)
Using RegExr: Learn, Build, & Test RegEx to explain this regular expression:
^ Beginning. Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled.
\s Whitespace. Matches any whitespace character (spaces, tabs, line breaks).
* Quantifier. Match 0 or more of the preceding token.
( Capturing group #1. Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.
. Dot. Matches any character except line breaks.
* Quantifier. Match 0 or more of the preceding token.
? Lazy. Makes the preceding quantifier lazy, causing it to match as few characters as possible.
)
\s Whitespace. Matches any whitespace character (spaces, tabs, line breaks).
* Quantifer. Match 0 or more of the preceding token.
\( Escaped character. Matches a "(" character (char code 40).
. Dot. Matches any character except line breaks.
* Quantifer. Match 0 or more of the preceding token.
\) Escaped character. Matches a ")" character (char code 41).
( Capturing group #2. Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.
. Dot. Matches any character except line breaks.
* Quantifier. Match 0 or more of the preceding token.
? Lazy. Makes the preceding quantifier lazy, causing it to match as few characters as possible.
)
\s Whitespace. Matches any whitespace character (spaces, tabs, line breaks).
* Quantifer. Match 0 or more of the preceding token.
$ End. Matches the end of the string, or the end of a line if multilline flag (m) is enabled.