in reply to Re^2: Regex word boundries
in thread Regex word boundries

One thing I did notice in your code (and after runnning) was that the word count seems to be just 1.

Sorry,
my $word_count = () = split(' ', $file);
should be
my $word_count = split(' ', $file);

I now have another problem in that some terms are still not picked up

\b matches between \w\W, \W\w, ^\w and \w\z. As such, the second \b won't match in 'h(2)O(2) water' =~ '/\b\Qh(2)O(2)\Q\b/. () is a \W, and so is the following space.) Perhaps this will do the trick:

/(?:\W|^)\Q$term\E(?:(?=\W)|\z)/

I think the following would be faster, but it would count a repeated term as one:

/(?:\W|^)\Q$term\E(?:\W|\z)/

If you want the match to be case-insensitive, one solution is to use the i modifier on your match.

Replies are listed 'Best First'.
Re^4: Regex word boundries
by MonkPaul (Friar) on Oct 29, 2007 at 15:24 UTC
    Thank you. That seemed to do the trick.

    I was wondering if you could possibly explain the regex you have used. I am trying now to identify one occurance of the term in a line of text so that I can work out the inverse document frequency (IDF).

    So far I have worked out that you are looking for the term, using a non-capturing means (?:pattern), i.e. (?:\W). I haven't a clue what this actually does, nor about the part after \E ..... (?:(?=\W).

    I know that the (?=\W) is a regex to look-ahead of a non-word, but not sure what the outer ?: is doing.

    cheers,
    MonkPaul.

      • Non-capturing parens ((?:...)) are just like parens ((...)) in Perl code. The alter precedence.

        # Matches strings that include "ab" or "cd" /ab|cd/ # Matches strings that include "abd" or "acd". /a(?:b|c)d/
      • (?=...) performs a match, but leaves pos unchanged after the match.

        local $_ = 'foo bar bar baz'; my $term = 'bar'; my $num_matches = () = /(?:\W|^)\Q$term\E(?:\W|\z)/g; print("$num_matches\n"); # 1: foo[ bar ]bar baz my $num_matches = () = /(?:\W|^)\Q$term\E(?:(?=\W)|\z)/g; print("$num_matches\n"); # 2: foo[ bar][ bar] baz