I'm trying to find a way to replace square brackets into apostrophes for some subtitle files, but only for cases when these square brackets do not contain a whole sentence having the square brackets at the beginning & end of the line.
These lines would have square brackets changed into apostrophes:
[que] vão levar [vocês]
ao [limite].
While these would not:
[Vamos começar]
[com algo simples.]
I came up with the following regex command
(?!^\[.*?\]$)(\[.*?\])
That uses negative lookahead to find lines starting with [ and ending with ], while using the inside question mark character ? as an operator to prevent selection of line with extra square brackets.
Unfortunately, this does not seem to work. What am I doing wrong in here?
You may match the lines that start with [ and end with ] and have no [ and ] and capture into Group 1, and only match other [ and ] and replace using a conditional replacement pattern:
Find what: ^(\[[^][\r\n]*\])$|[][]
Replace with: (?1$1:')
Search pattern details:
^ - start of line
(\[[^][\r\n]*\]) - Group 1 capturing a [, then 0 or more characters other than ], [, \r or \n and then ] at the...
$ - end of line
| - or
[][] - a [ or ]
Replacement pattern details:
(?1 - Did the Group 1 match? If yes,
$1 - use the Group 1 contents
: - or
' - a single apostrophe
) - end of the conditional pattern.
Related
I have a text like this;
[Some Text][1][Some Text][2][Some Text][3][Some Text][4]
I want to match [Some Text][2] with this regex;
/\[.*?\]\[2\]/
But it returns [Some Text][1][Some Text][2]
How can i match only [Some Text][2]?
Note : There can be any character in Some Text including [ and ] And the numbers in square brackets can be any number not only 1 and 2. The Some Text that i want to match can be at the beginning of the line and there can be multiple Some Texts
JSFiddle
The \[.*?\]\[2\] pattern works like this:
\[ - finds the leftmost [ (as the regex engine processes the string input from left to right)
.*? - matches any 0+ chars other than line break chars, as few as possible, but as many as needed for a successful match, as there are subsequent patterns, see below
\]\[2\] - ][2] substring.
So, the .*? gets expanded upon each failure until it finds the leftmost ][2]. Note the lazy quantifiers do not guarantee the "shortest" matches.
Solution
Instead of a .*? (or .*) use negated character classes that match any char but the boundary char.
\[[^\]\[]*\]\[2\]
See this regex demo.
Here, .*? is replaced with [^\]\[]* - 0 or more chars other than ] and [.
Other examples:
Strings between angle brackets: <[^<>]*> matches <...> with no < and > inside
Strings between parentheses: \([^()]*\) matches (...) with no ( and ) inside
Strings between double quotation marks: "[^"]*" matches "..." with no " inside
Strings between curly braces: \{[^{}]*} matches "..." with no " inside
In other situations, when the starting pattern is a multichar string or complex pattern, use a tempered greedy token, (?:(?!start).)*?. To match abc 1 def in abc 0 abc 1 def, use abc(?:(?!abc).)*?def.
You could try the below regex,
(?!^)(\[[A-Z].*?\]\[\d+\])
DEMO
I'm trying to retrieve preceding TableNames before brackets:
=IFERROR(INDEX(RepositoriesQ[ContentRepository];MATCH(1&2;RepositoriesQ[Url]&RepositoriesQ[Credentials];0));"something")
I found the way of getting all strings between brackets:
\[(.*?)\]
but what i want to get is all strings preceding column names in brackets.
So as result i should get 3 matches here:
RepositoriesQ[ContentRepository]
RepositoriesQ[Url]
RepositoriesQ[Credentials]
You can use
\w+\[[^\][]*]
See the regex demo. Details:
\w+ - one or more word chars
\[ - a [ char
[^\][]* - zero or more chars other than [ and ]
] - a ] char.
I'm actually creating a discord bot and I'm trying to match some command options and I have a problem getting the value between the square brackets. (if there is)
I've already tried to add a ? to match one or more of these but it's not working, searching about how I could match between two characters but found nothing that helped me.
Here is the pattern I've got so far : https://regexr.com/4icgi
and here it is in text : /[+|-](.+)(\[(.+)\])?/g
What I expect it to do is from an option like that : +user[someRandomPeople]
to extract the parameter user and the value someRandomPeople and if there is no square brackets, it will only extract the parameter.
You may use
^[+-](.*?)(?:\[(.*?)\])?$
Or, if there should be no square brackets inside the optional [...] substring at the end:
^[+-](.*?)(?:\[([^\][]*)\])?$
Or, if the matches are searched for on different lines:
^[+-](.*?)(?:\[([^\][\r\n]*)\])?$
See the regex demo and the regex graph:
Details
^ - start of string
[+-] - + or - (note that | inside square brackets matches a literal | char)
(.*?) - Group 1: any 0 or more chars other than line break chars as few as possible
(?:\[(.*?)\])? - an optional sequence of
\[ - a [ char
(.*?) - Group 2: any 0 or more chars other than line break chars as few as possible ([^\][]* matches 0 or more chars other than [ and ])
\] - a ] char
$ - end of string.
I want extract the only the value between square brackets in a given line.
From the text
TID: [-1] [] [2019-07-29 10:18:41,876] INFO
I want to extract the first occurrence between square brackets which is -1.
I tried using
(?<Ten ID>((^(?!(TID: )))*((?<=\[).*?(?=\]))))
but it gives
-1, ,2019-07-29 10:18:41,876
as resultant matches.
How to capture only the first occurrence?
You can access the regex editor here.
Regarding
Is there a solution without group capturing?
You may use
/\bTID:\s*\[\K[^\]]+(?=\])/
See the Rubular demo
Details
\bTID: - whole word TID followed with a colon
\s* - 0+ whitespace chars
\[ - a [ char
\K - match reset operator that discards the text matched so far
[^\]]+ - one or more chars other than ]
(?=\]) - a positive lookahead that makes sure there is a ] char immediately to the right of the current location.
You might capture the first occurrence in the named capturing group using a negated character class:
\ATID: \[(?<Ten ID>[^\[\]]+)\]
\A Start of string
TID: Match literally
\[ Match [
(?<Ten ID> Named capturing group Ten ID
[^\[\]]+ Match not [ or ] using a negated character class
) Close group
\] Match ]
See https://rubular.com/r/4Hc80yrDxGVgvi
str = “TID:] [-1] [] [2019-07-29 10:18:41,876] INFO”
i1 = str.index(‘[‘)
#=> 6
i2 = str.index(‘]’, i1+1)
#=> 9
i1.nil? || i2.nil? ? nil : str[i1+1..i2-1]
#=> “-1”
I need to extract an expression between brackets that can include everything but not an non-escaped closed bracket.
For example, the regexp from [aaa\]bbbbbb] should give as result : aaa\]bbbbbb.
I tried this : \[([^(?<!\\)\]]*)\] but that fail.
Any hints?
You may use
\[([^\]\[\\]*(?:\\.[^\]\[\\]*)*)]
Or - if there may be any non-escaped [ in-between non-escaped [ and ] (e.g. [a[\[aa\]bbbbbba\[aabbbbbb]), take out the \[:
\[([^\]\\]*(?:\\.[^\]\\]*)*)]
See the regex demo 1 and regex demo 2. It is an unrolled variant of a \[((?:[^][\\]|\\.)*)] regex.
Details:
\[ - a [
([^\]\[\\]*(?:\\.[^\]\[\\]*)*) - Group 1 capturing:
[^\]\[\\]* - zero or more chars other than [, ] and \ (in some regex flavors, you may write it without escapes - [^][\\]*)
(?:\\.[^\]\[\\]*)* - zero or more sequences of:
\\. - any escaped sequence (\ and any char other than line break chars
[^\]\[\\]* - zero or more chars other than [, ] and \
] - a closing ].
This is the simplest regex that (I think) works:
\[(.*?)(?<!\\)\]
which captures the bracketed text as group 1.
See live demo.