20

The man page for GNU's less utility says the following about searching:

/pattern
    Search forward in the file for the N-th line containing the pattern.  N
    defaults to 1.  The pattern is a regular expression, as recognized by the
    regular expression library supplied by your system.

I use less on all sorts of systems: my personal Ubuntu laptop, my CentOS cloud server, under Cygwin at work, etc. I keep wanting to do things like negative look-aheads and other fancy stuff, but I don't know what regex syntax to use. How do I find out?

2

3 Answers 3

11

It is a compile time parameter. The ./configure script of less knows the with-regex=LIB param.

This is a quote from README of the upstream package:

--with-regex=lib

     Specifies the regular expression library used by less for pattern
     matching.  The default is "auto", which means the configure program 
     finds a regular expression library automatically.  Other values are:
        posix          Use the POSIX-compatible regcomp.
        pcre           Use the PCRE library.
        regcmp         Use the regcmp library.
        re_comp        Use the re_comp library.
        regcomp        Use the V8-compatible regcomp.
        regcomp-local  Use Henry Spencer's V8-compatible regcomp
                       (source is supplied with less).

So you would need to know how less was './configured'. I have investigated this on Debian / Ubuntu. They use the POSIX regex lib.

I'm still searching for a way to detect it dynamically by a script... :)


Update: The only thing I've managed so far was to detect whether less uses pcre regexes or not. If less was configured using --with-regex=pcre it is linked against the libpcre.so shared library:

#!/bin/bash

# ldd prints out the shared libraries a binary is linked to.
# This can be used to check if less is linked against libpcre
if ldd "$(which less)" | grep 'libpcre\.so' ; then   
    echo "less uses pcre regex syntax"
else 
    echo "less uses non pcre regex syntax"
    # ... more checks should follow. currently trying to find a way
fi
8
  • Thanks. At least I know now where to look, but I'm sure further updates from you would be helpful. From some quick web searches, it looks like pcre is the only one with the kind of advanced features I want—would you agree? Commented Feb 6, 2013 at 2:11
  • if you prefer them, ok. (me too:)) I'm currently thinking about hashing the less binary, compiled with several --with-regex options, and then just compare the hashes to say which regex lib is used (at a certain system). But if you are happy with my answer, I'll be too! :)
    – hek2mgl
    Commented Feb 6, 2013 at 2:19
  • I'm satisfied with your answer, since it answers my question. Such a hash would make me happier, though. ;-) Let me know if there's anything I can do to either help, or incentivise you. I'll keep an eye on the email address in my StackExchange profile for now. Commented Feb 8, 2013 at 4:44
  • @MichaelScheper The email address isn't displayed. (seems private) If you wan't you can contact me via the email displayed on my homepage. You'll find it when looking at my profile on SO
    – hek2mgl
    Commented Feb 8, 2013 at 16:36
  • I learnt the provenance of some of these libraries in an appendix of the manual for GNU gcal. Could you add descriptions to your answer?
    – crw
    Commented Aug 4, 2016 at 13:08
7

I don't know if this works in all cases (older versions/different systems) but I was able to find this info using less --version:

less 458 (GNU regular expressions)
Copyright (C) 1984-2012 Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less

So it's GNU regex syntax...

And after compiling a newer version with --with-regex=pcre I got

less 481 (PCRE regular expressions)
...

Update

Thanks to crw for checking. This solution does appear to be version-specific. After compiling available source code at greenwoodsoftware (in Linux), I found that it does not work for versions 436 (released 25 Jul 2009) and earlier. It starts working by at least 451 (released 4 Sep 2012) and later. (Versions in between these were not available for download).

2
  • Wow, right under our noses. If there's a way to verify that this is always the case, I'd mark this the 'accepted' answer. Commented Aug 4, 2016 at 13:34
  • 2
    Unfortunately, I found the world to be more complex than I'd believed... See update above. Commented Aug 5, 2016 at 19:40
0

The suggested answer to observe the output of less --version didn't address my situation on Solaris 10 -- the first two lines read:

less 436
Copyright (C) 1984-2009 Mark Nudelman

I cannot see an obvious regex library in the list of dynamic dependencies:

$ ldd /usr/bin/less
        libcurses.so.1 =>        /lib/libcurses.so.1
        libc.so.1 =>     /lib/libc.so.1
        libm.so.2 =>     /lib/libm.so.2
        /lib/libm/libm_hwcap1.so.2
        /platform/sun4v/lib/libc_psr.so.1

man libc indicates that multiple regex interfaces are offered by the library: regcmp, re_comp and regcomp.

By running elfdump against the binary, I can see references to the symbol regcomp:

$ elfdump /usr/bin/less | egrep -i 'posix|pcre|regcmp|re_comp|regcomp|regcomp-local'
     [452]  0x0003d6a0 0x00000000  FUNC GLOB  D    0 UNDEF          regcomp
            [452]       regcomp
  R_SPARC_JMP_SLOT            0x3d6a0          0  .rela.plt      regcomp

If this is a link to the regcomp regex compiler function, then the README-text in the answer from @hek2mgl suggests that this less binary might use POSIX regexes (or Spencer V8 regexes, if compiled into the binary?).

The man page for regcomp(3C) reads:

DESCRIPTION
     These functions interpret basic and extended regular expres-
     sions (described on the regex(5) manual page).

Experimenting with searches in less, I've discovered that the regex repetition operator {...} works without backslash escapes. The manpage for regex(5) on my system defines this as Extended Regular Expression (ERE) syntax.

Lastly, I've found several interesting descriptions of the interfaces of various regex engines, summarised below:

Engine            Interface
----------------  --------------------------------
GNU               re_compile_pattern() and regex.h
PCRE              pcre_compile and pcre.h / pcre2_compile and pcre2.h
POSIX             regcomp() and regex.h
Henry Spencer V8  regcomp() and regexp.h
BSD               re_comp()
System V          regcmp()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.