Regex with backslashes in a collection

24 views
Skip to first unread message

JohnBeckett

unread,
Aug 13, 2022, 3:24:19 AM8/13/22
to vim_use
I'm using Vim 8 or 9 with 'magic' and re=0 (same using re=1).

I want to find "\[" or "\]" in text like:
abc\[def\]ghi\\xy\z

The following pattern also find the two backslashes.
\\[\[\]]

Help says that a backslash in a collection (if not followed
by certain characters) just means a backslash.
:help /\]

Why does the pattern find two backslashes (but not one)?

These patterns find only "\]" (good):
\\[\]]
\\[]]

But this finds "\[" and "\\" (not "\"):
\\[\[]

John

aro...@vex.net

unread,
Aug 13, 2022, 11:34:48 AM8/13/22
to vim...@googlegroups.com
> I'm using Vim 8 or 9 with 'magic' and re=0 (same using re=1).
>
> I want to find "\[" or "\]" in text like:
> abc\[def\]ghi\\xy\z
>

The problem may lie in the boundary of a character class, [ and ] and the
rules about its content.
Does the regex interpreter read [\[] as 2 characters, "\ or ]" or as one
"literal ]"; or "literal \"? How is the following "]" parsed?

There may be enough ambiguity in the definitions that tests will only be
valid in the particular interpreter.

Gary Johnson

unread,
Aug 14, 2022, 4:40:59 AM8/14/22
to vim...@googlegroups.com
On 2022-08-13, arocker wrote:
> > I'm using Vim 8 or 9 with 'magic' and re=0 (same using re=1).
> >
> > I want to find "\[" or "\]" in text like:
> > abc\[def\]ghi\\xy\z
> >
>
> The problem may lie in the boundary of a character class, [ and ] and the
> rules about its content.
> Does the regex interpreter read [\[] as 2 characters, "\ or ]" or as one
> "literal ]"; or "literal \"? How is the following "]" parsed?

According to ":help /\]", [\[] is a collection containing two
characters, \ and [.

Regards,
Gary

JohnBeckett

unread,
Aug 14, 2022, 6:25:55 AM8/14/22
to vim_use
> [\[] is a collection containing two characters, \ and [

Yes, that's what my test shows. The problem is that the pattern \\[\[] finds "\\" and "\[" but not "\".

John

Tommy Bollman

unread,
Aug 14, 2022, 8:44:14 AM8/14/22
to vim...@googlegroups.com
/\(\\[\|\\\]\)

No magic or anything set as you can see. Matches \[ or \]. 

Regards.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/88494085-43a9-46d8-a58b-f35981545112n%40googlegroups.com.

Gary Johnson

unread,
Aug 14, 2022, 1:27:27 PM8/14/22
to vim_use
Right. Your pattern says to find a \ followed by one of the
items of the collection containing \ and [. So, it will match
either of

\\
\[

but not a single \ unless it is immediately followed by a [. If you
want it to also match a single \, you could use this:

\\[\[]\?

which will match \ followed by zero or one of \ or [.

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages