This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Lisp and Regex

8 posts · 7207 views

Hi!

Im using a programme to get word/char-frequencies from a txt file, and im wondering if there is an easy way of searching through a string looking for a
regex pattern?

I would for example like to do something like this:
(defun check-word (word)
  (let ((s word))
    (setf s (string s))
    (search "[^A-Za-z]" s)))
to check that a word does not contain any other chars than A-Z or a-z, where "[^A-Za-z]" is the regular expression.

Anyone know if such a thing is possible to achive? :)

Re: Lisp and Regex

kroger wrote:you can use cl-ppcre, a portable regexp library for lisp:

http://www.weitz.de/cl-ppcre/
It probably won't matter for this usage, but it's important to note that PCRE stands for Perl-Compatible Regular Expression, and Perl's regular expressions are not regular expressions in the formal sense. This means that they cannot be implemented be implemented by finite state machines, and cannot offer the linear performance guarantees of true regular expressions. If that's what you need, don't use PCREs.

Re: Lisp and Regex

metageek wrote:Perl's regular expressions are not regular expressions in the formal sense. This means that they cannot be implemented be implemented by finite state machines
Does that mean they're more flexible than normal regular expressions - they can classify context-free/sensitive languages?

Re: Lisp and Regex

Exolon wrote:
metageek wrote:Perl's regular expressions are not regular expressions in the formal sense. This means that they cannot be implemented be implemented by finite state machines
Does that mean they're more flexible than normal regular expressions - they can classify context-free/sensitive languages?
Yes. For example, if I am not mistaken, the Perl regex
(a*)b\1
matches {a^n b a^n | n ∈ ℕ}, which is not regular.

Re: Lisp and Regex

Exolon wrote:
metageek wrote:Perl's regular expressions are not regular expressions in the formal sense. This means that they cannot be implemented be implemented by finite state machines
Does that mean they're more flexible than normal regular expressions - they can classify context-free/sensitive languages?
Wikipedia wrote: Many features found in modern regular expression libraries provide an expressive power that far exceeds the regular languages. For example, the ability to group subexpressions with parentheses and recall the value they match in the same expression means that a pattern can match strings of repeated words like "papa" or "WikiWiki", called squares in formal language theory. The pattern for these strings is (.*)\1. However, the language of squares is not regular, nor is it context-free. Pattern matching with an unbounded number of back references, as supported by numerous modern tools, is NP-hard.

Re: Lisp and Regex

Thanks guys, this makes sense :)