Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 'the regular expression library supplied by [my] system'?

Tags:

regex

linux

gnu

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?

like image 252
Michael Scheper Avatar asked Feb 05 '13 00:02

Michael Scheper


People also ask

What is regex library?

RegEx Library - a curated list of useful regular expressions for different programming languages.

What is this regular expression?

A Regular Expression (or Regex) is a pattern (or filter) that describes a set of strings that matches the pattern. In other words, a regex accepts a certain set of strings and rejects the rest.

Is regex a standard library?

The regular expressions library provides a class that represents regular expressions, which are a kind of mini-language used to perform pattern matching within strings. Almost all operations with regexes can be characterized by operating on several of the following objects: Target sequence.

What is regular expression used for?

Regular expressions are particularly useful for defining filters. Regular expressions contain a series of characters that define a pattern of text to be matched—to make a filter more specialized, or general.


2 Answers

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
like image 93
hek2mgl Avatar answered Oct 11 '22 12:10

hek2mgl


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).

like image 32
flyingfinger Avatar answered Oct 11 '22 11:10

flyingfinger