Re: regexp curly braces do not work

Eric Blake <[email protected]>
Newsgroups gmane.comp.gnu.m4.bugs
Organization Red Hat, Inc.
Message-ID <[email protected]>
On 7/30/20 1:24 AM, Hyunho Cho wrote:
> m4 (GNU M4) 1.4.18
> Operating System: Ubuntu 20.04 LTS
> Kernel: Linux 5.4.0-33-generic
> Architecture: x86-64
> 
> 
> sh$ m4 <<\@
> patsubst(`hello', `ll', `XX')
> patsubst(`hello', `l+', `XX')
> patsubst(`hello', `l\{2\}', `XX')
> patsubst(`hello', `l{2}', `XX')
> @
> heXXo
> heXXo
> hello
> hello

Unfortunately correct for m4 1.4.  The source code uses GNU regex.h, and 
does not bother to modify re_syntax or call re_set_syntax(), which means 
m4 defaults to RE_SYNTAX_EMACS (ie. all regex bits set to 0).  Looking 
at the various bits that can be set for re_syntax,

/* If this bit is set, either \{...\} or {...} defines an
      interval, depending on RE_NO_BK_BRACES.
    If not set, \{, \}, {, and } are literals.  */
# define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)

and since m4 isn't doing anything about that bit, no form of {} works.

At some point, a future m4 release will add the ability to set regex 
flavors (emacs, POSIX BRE, POSIX ERE, etc), and depending on the flavor 
chosen, either {} or \{\} will work.  But until that release happens, 
you are stuck with the existing m4 being limited in expressive power.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.