Regular expression to match a regular expression inside square brackets - regex

I have a string that contains a regular expression within square brackets, and can contain more than 1 item inside square brackets. below is an example of a string I'm using:
[REGEX:^([0-9])*$][REGEXERROR:That value is not valid]
In the example above, I'd like to match for the item [REGEX:^([0-9])*$], but I can't figure out how.
I thought I'd try using the regular expression \[REGEX:.*?\], but it matches [REGEX:^([0-9] (ie; it finishes when it finds the first ]).
I also tried \[REGEX:.*\], but it matches everything right to the end of the string.
Any ideas?

Suppose you are using PCRE, this should be able to find nested brackets in regular expressions:
\[REGEX:[^\[]*(\[[^\]]*\][^\[]*)*\]
This technique is called unrolling. The basic idea of this regex is:
match the starting brackets
match all characters that are not brackets
match one brackets
match all trailing characters that are not brackets
then repeat 3 and 4 until the last closing bracket comes
Explanation with free-space:
\[ # start brackets
REGEX: # plain match
[^\[]* # match any symbols other than [
( # then match nested brackets
\[ # the start [ of nested
[^\]]* # anything inside the bracket
\] # closing bracket
[^\[]* # trailing symbols after brackets
)* # repeatable
\] # end brackets
Reference: Mastering Regular Expression

Related

Changing Parantheses to Square Brackets With Regular Expressions

Under Visual Studio 2019 I am trying to replace Parantheses with Square Brackets
For example: fld(126) to fld[126]
Using this regular expression
fld[\(][0-9]*[\)]
matches good what I look for in the code.
But \2 replaces everything between parentheses with '\2' instead of leaving what exists there before.
Any help would be appreciated...
Your example RegEx shows you only want to change the brackets for the function fld.
Use
Find: fld\((\d*)\)
Replace: fld[$1]
If the content between the brackets can be more than numbers use
Find: fld\(([^)]*)\)
Replace: fld[$1]
Try the following find and replace, in regex mode:
Find: ([^(\s]+)\(([^)]+)\)(?!\S)
Replace: $1[$2]
Demo
Here is an explanation of the regex pattern:
([^(\s]+) match AND capture the leading 'fld' term, in $1
this is given by all non '(' and whitespace characters
\( match a literal (
([^)]+) match AND capture the content inside parentheses, in $2
\) match a literal )
(?!\S) assert that what follows the closing ) is a boundary or whitespace

RegEx for replacing alphanumeric chars in Notepad++ [duplicate]

This question already has an answer here:
Notepad++ regular expression replace
(1 answer)
Closed 3 years ago.
I want to upgrade a project from one version to another so, that in need to change thousands of lines with same pattern.
Example:
From this
$this->returnData['status']
To this
$this->{returnData['status']}
By using following regex i found all the matches but unable to replace with braces.
->[a-zA-z]{5,15}\['[a-zA-z]{5,15}'\]
I used following to replace
->\{[a-zA-z]{5,15}\['[a-zA-z]{5,15}'\]\}
Try using the following find and replace, in regex mode:
Find: \$this->([A-Za-z]{5,15}\['[A-Za-z]{5,15}'\])
Replace: $this->{$1}
Demo
The regex matches says to:
\$this-> match "$this->"
( then match and capture
[A-Za-z]{5,15} 5-15 letters
\[ [
'[A-Za-z]{5,15}' 5-15 letters in single quotes
\] ]
) stop capture group
The replacement is just $this-> followed by the first capture group, but now wrapped in curly braces.
If you want to just match the arrow operator, regardless of what precedes it, then just use this pattern, and keep everything else the same:
->([A-Za-z]{5,15}\['[A-Za-z]{5,15}'\])
You could also use negated character classes and use \K to forget the match. In the replacement use $0 to insert the full match surrounded by { and }
Match:
->\K[^[\n]+\['[^\n']+']
That will match
-> match ->
\K Forget what was matched
[^[\n]+ Match not [ or newline 1+ times or exactly [a-zA-Z]{5,15} instead of [^[\n]+
\[ Match [
'[^\n']+' Match from ', 1+ times not ' or newline and then ' or exactly [a-zA-Z]{5,15} instead of [^\n']+
] Match literally
Regex demo
Replace with:
{$0}
Result:
$this->{returnData['status']}
The exacter version would look like:
->\K[a-zA-Z]{5,15}\['[a-zA-Z]{5,15}']
Regex demo
Note that [a-zA-z] matches more than [a-zA-Z]

Negative lookbehind and square brackets

I 'd like to create a regex that matches unmatched right square brackets. Examples:
]ichael ==> match ]
[my name is Michael] ==> no match
No nested pairs of of square brackets occur in my text.
I tried to use negative lookbehind for that, more specifically I use this regex: (?<!\[(.)+)\] but it doesn't seem to do the trick.
Any suggestions?
Unless you are using .NET, lookbehinds have to be of fixed length. Since you just want to detect whether there are any unmatched closing brackets, you don't actually need a lookbehind though:
^[^\[\]]*(?:\[[^\[\]]*\][^\[\]]*)*\]
If this matches you have an unmatched closing parenthesis.
It's a bit easier to understand, if you realise that [^\[\]] is a negated character class that matches anything but square brackets, and if you lay it out in freespacing mode:
^ # start from the beginning of the string
[^\[\]]* # match non-bracket characters
(?: # this group matches matched brackets and what follows them
\[ # match [
[^\[\]]* # match non-bracket characters
\] # match ]
[^\[\]]* # match non-bracket characters
)* # repeat 0 or more times
\] # match ]
So this tries to find a ] after matching 0 or more well-matched pairs of brackets.
Note that the part between ^ and ] is functionally equivalent to Tim Pietzker's solution (which is a bit easier to understand conceptually, I think). What I have done, is an optimization technique called "unrolling the loop". If your flavor provides possessive quantifiers, you can turn all * into *+ to increase efficiency even further.
About your attempt
Even if you are using .NET, the problem with your pattern is that . allows you to go past other brackets. Hence, you'd get no match in
[abc]def]
Because both the first and the second ] have a [ somewhere in front of them. If you are using .NET, the simplest solution is
(?<!\[[^\[\]]*)\]
Here we use non-bracket characters in the repetition, so that we don't look past the first [ or ] we encounter to the left.
You don't need lookaround at all (and it would be difficult to use it most languages don't allow unlimited-length lookbehind assertions):
((?:\[[^\[\]]*]|[^\[\]]*)*+)\]
will match any text that ends in a closing bracket unless there's a corresponding opening bracket before it. It does not (and according to your question doesn't need to) handle nested brackets.
The part before the ] can be found in $1 so you can reuse it later.
Explanation:
( # Match and capture in group number 1:
(?: # the following regex (start of non-capturing group):
\[ # Either a [
[^\[\]]* # followed by non-brackets
\] # followed by ]
| # or
[^\[\]]* # Any number of non-bracket characters
)*+ # repeat as needed, match possessively to avoid backtracking
) # End of capturing group
\] # Match ]
This should do it:
'^[^\[]*\]'
Basically says pick out any closing square bracket that doesn't have an open square bracket between it and the beginning of the line.
\](.*)
Will match on everything after the ]:
]ichael -> ichael
[my name is Michael] ->

Eclipse regex find and replace

I want to replace the below statement
ImageIcon("images/calender.gif");
with
ImageIcon(res.getResource("images/calender.gif"));
Can anyone suggest a regex to do this in eclipse.Instead of "calender.gif" any filename can come.
You can find this pattern (in regex mode):
ImageIcon\(("[^"]+")\)
and replace with:
ImageIcon(res.getResource($1))
The \( and \) in the pattern escapes the braces since they are to match literally. The unescaped braces (…) sets up capturing group 1 which matches the doublequoted string literal, which should not have escaped doublequotes (which I believe is illegal for filenames anyway).
The […] is a character class. Something like [aeiou] matches one of any of the lowercase vowels. [^…] is a negated character class. [^aeiou] matches one of anything but the lowercase vowels.
The + is one-or-more repetition, so [^"]+ matches non-empty sequence of everything except double quotes. We simply surround this pattern with " to match the double-quoted string literal.
So the pattern breaks down like this:
literal( literal)
| |
ImageIcon\(("[^"]+")\)
\_______/
group 1
In replacement strings, $1 substitutes what group 1 matched.
References
regular-expressions.info
Character Class, Repetition, Brackets
Examples/Programming Constructs - Strings - has patterns for strings that may contain escaped doublequotes
Ctrl-F
Find: ImageIcon\("([^\"]*)"\);
Replace with: ImageIcon(res.getResource("\1"));
Check Regular Expressions checkbox.

regex: match white-spaces that do not enclosed in []

For example, for this string,
div.img-wrapper img[title="Hello world"]
I want to match the first space but not the second space (which is enclosed in []). What is the regex?
The following expression will do the job by using a look ahead assertion.
_(?>[^[\]]*(\[|$))
The underscore represents a space. This expression does not support nested brackets because regular expression are not powerful enough to express nested matched structures.
_ Match the space and
(?> assert that it is not inside brackets
[^[\]]* by matching all characters except brackets
( followed by either
\[ an opening bracket (a space inside brackets
will have a closing bracket at this position)
| or
$ or no more characters (end of line).
)
)
UPDATE
Here is another (and more beautiful) solution using a negative look ahead assertion.
_(?![^[\]]*])
It asserts that the next bracket after a space is not a closing bracket.