Text:
[A]I\'m an example text [] But I want to be included [[]]
[A]I\'m another text without a second part []
Regex:
\\[A\\][\\
You may use
\[A][\s\S]*?(?=\[A]|$)
See the regex demo.
Details
\[A]
- a [A]
substring[\s\S]*?
- any 0+ chars as few as possible(?=\[A]|$)
- a location that is immediately followed with [A]
or end of string.In C#, you actually may even use a split operation:
Regex.Split(s, @"(?!^)(?=\[A])")
See this .NET regex demo. The (?!^)(?=\[A])
regex matches a location in a string that is not at the start and that is immediately followed with [A]
.
If instead of A
there can be any letter, replaces A
with [A-Z]
or [A-Z]+
.
I have changed your regex (actually simpler) to do what you want:
\[A\].*\[?\[\]\]?
It starts by matching the '[A]
', then matches any number of any characters (greedy) and finally one or two '[]
'.
Edit:
This will prefer double Square brackets:
\[A\].*(?:\[\[\]\]|\[\])