Regex to capture string inside parenthesis - regex

I have tried
[^\(]\(.*\)
but in a string like
Tamara(PER) Jorquiera
The pattern returns
a(PER)
How can I get only the text inside parenthesis, assuming the open and close parenthesis occur once?

[^\(]\(.*\)
It matches any character that is not ( (a in the example).. and a literal ( then everything till )
You can use the following:
\([^)]*\) // this will match only the text between parentheses
// including parantheses
If you want only the text use lookahead and lookbehinds:
(?<=\()[^)]*(?=\))
See DEMO

Use following regex :
\(([^)]*?)\)
Demo
Note that you don't need [^\(] out side of the parenthesis just use
[^)]*? within a capture group.
[^)]*? is a non-greedy regex that match anything except closing parenthesis.

Related

Regex to match within brackets in vim

Let's say I have the following text:
new_item['uid']
And I want to capture everything within the [ ... ]. So in this case grab the 'uid'. Normally I could use something like:
\[([^\]]+)]
To match this (start with the opening bracket and get everything until the closing bracket). But without the character classes, or negated character class in vim, how would I do something similar?
If you want to have a capture group with (..), you need the verymagic mode, otherwise you have to escape the ( and ), similar to the BRE.
So both give the matched part in \1:
\[\([^]]*\)
and (\v tells vim to match in verymagic mode)
\v\[([^]]*)
You could use .\{-} in place of .*? to make a lazy dot match:
\[(.\{-})\]
in addition to the other answers, you could use \v\[\zs.{-}\ze\] to only highlight the text within \zs and \ze, see :h \ze

What is the syntax in TextMate to insert a new line '\n' before every capital letter? [duplicate]

I am using TextMate to replace expression [my_expression] consisting in characters between open and closed brackets by {my_expression}; so I tried to replace
\[[^]]*\]
by
{$1}
The regex matches the correct expression, but the replacement gives {$1}, so that the variable is not recognised. Can someone has an idea ?
You forgot to escape a character, [^]] should be [^\]].
You also need a capture group. $1 is back-referencing the 1st Capture Group, and you had no capture groups, so use the following Regex:
\[([^\]]*)\]
This adds () around [^\]]*, so the data inside the [] is captured. For more info, see this page on Capture Groups
However, this RegEx is shorter:
\[(.*?)\]
Also substituting with {$1}
Live Demo on Regex101
Use a capturing group (...):
\[([^\]]*)\]
The $1 is a backreference to the text enclosed with [...].
Here is the regex demo and also Numbered Backreferences.
Also, the TextMate docs:
1. Syntax elements
(...) group
20.4.1 Captures
To reference a capture, use $n where n is the capture register number. Using $0 means the entire match.
And also:
If you want to use [, -, ] as a normal character in a character class, you should escape these characters by \.

REGEX PCRE - Avoid string and the first instance of any string within a parenthesis

Here is an example of the sort of strings I am working with:
[TEXT1] TEXT EXAMPLE HERE (ROBERT_01) MORE TEXT HERE POSSIBLY SOME NUMBERS 9999 THEN TEXT (JIMBO_01)
What I am trying to do is remove everything AFTER the first set of brackets i.e. "(ROBERT_01)".
I can get it to work whilst explicitly stating the text itself e.g.:
(?<=TEXT EXAMPLE HERE \(ROBERT_01\)).+
However this is problematic as the string between the parenthesis isn't always "ROBERT_01".
EDIT: Note, I am using the FME transformer 'StringReplacer' which utilises Regex.
What you might do is match not an opening parenthesis one or more times until you encounter an opening parenthesis using a negated character class [^(]+. Then match the opening parenthesis \(, match not a closing parentheses zero or more times [^)]* and then match the closing parenthesis \).
That will match [TEXT1] TEXT EXAMPLE HERE (ROBERT_01)
Then use \K so previously consumed characters are no longer included in the final match.
Finally match any character zero or more times .* and would match:
MORE TEXT HERE POSSIBLY SOME NUMBERS 9999 THEN TEXT (JIMBO_01)
Regex
[^(]+\([^)]*\)\K.*
Regular expression can be
/\(.*?\)(.*?)$/gm
This will capture the text between parentheses on first match and the remaining text on the second,
See it at online regex tester
But maybe you need this other which just matches text from the beginning of string to the first text enclosed by parentheses
/^(.*?)\s?\(.*\).*$/
See it also at online regex tester

Replacing with backreference in TextMate issue

I am using TextMate to replace expression [my_expression] consisting in characters between open and closed brackets by {my_expression}; so I tried to replace
\[[^]]*\]
by
{$1}
The regex matches the correct expression, but the replacement gives {$1}, so that the variable is not recognised. Can someone has an idea ?
You forgot to escape a character, [^]] should be [^\]].
You also need a capture group. $1 is back-referencing the 1st Capture Group, and you had no capture groups, so use the following Regex:
\[([^\]]*)\]
This adds () around [^\]]*, so the data inside the [] is captured. For more info, see this page on Capture Groups
However, this RegEx is shorter:
\[(.*?)\]
Also substituting with {$1}
Live Demo on Regex101
Use a capturing group (...):
\[([^\]]*)\]
The $1 is a backreference to the text enclosed with [...].
Here is the regex demo and also Numbered Backreferences.
Also, the TextMate docs:
1. Syntax elements
(...) group
20.4.1 Captures
To reference a capture, use $n where n is the capture register number. Using $0 means the entire match.
And also:
If you want to use [, -, ] as a normal character in a character class, you should escape these characters by \.

Replacing a string in Sublime

I'd like to replace this
y[100] with this Ith(y,100) in Sublime3.
I've got the regular expression \by[\d+] in find what and Ith(y,$1) in replace with, but it doesn't work. It finds what to replace correctly but just replaces it with Ith(y, )
You need to pick the \d+ inside () to capture in group($1)
\by[(\d+)]
Also, you may need to to escape the [ and ] characters here.
\by\[(\d+)\]
You have to capture the data you want to backreference: to do that you have to use unescaped parenthesis (...), ie capturing groups. Also, [...] are character classes, a special character in regex that you need to escape.
Try replacing
\by\[(\d+)\]
with
Ith(y, \1)
You need to escape the special characters and capture the number in a capturing group group. Try this regex:
\by\[(\d+)]\b
And replace with
Ith(y, $1)
Online explanation and demonstration: http://regex101.com/r/nX3yJ9