in reply to Defining Characters in Word Boundary?

/\b/ is equivalent to /(?<=\w)(?!\w)|(?<!\w)(?=\w)/. Feel free to replace \w with a character class.

It would be tedious to have to write \\$keyword([^a-zA-Z]) and then have to substitute back $1 (because I do not want it eaten).

Don't eat it if you don't want add it back. Equivalent without eating:

\\$keyword(?=[^a-zA-Z])

But you surely meant

\\$keyword(?![a-zA-Z])

In general, it's easier to extract the keyword, then check if it's the one you want.

\\([a-zA-Z]+)

Replies are listed 'Best First'.
Re^2: Defining Characters in Word Boundary?
by Jim (Curate) on Jan 20, 2011 at 01:30 UTC
    In general, it's easier to extract the keyword, then check if it's the one you want.

    I agree wholeheartedly. Since the LaTeX name constraint is exact and well-understood (the characters 'a' through 'z' and the characters 'A' through 'Z'), you simply need to match just those characters. Explicitly matching the right-hand boundary isn't necessary.

Re^2: Defining Characters in Word Boundary?
by iaw4 (Monk) on Jan 20, 2011 at 14:00 UTC
    thanks. this is what I needed to learn. I did not know the extended regex expressions in the camel book (i.e., (?...) sequences), chapter 5, table 5.6. is there a meaningful difference between (?!a-z) and (?=^a-z)? is the former recommended? /iaw
      Compare
      'ab' =~ /a(?!a)/ 'a' =~ /a(?!a)/
      and
      'ab' =~ /a(?=[^a])/ 'a' =~ /a(?=[^a])/
      is there a meaningful difference between (?![a-z]) and (?=[^a-z])? is the former recommended?

      Yes, they're different regular expression patterns that match different things. (?![a-z]) asserts "not followed by any of the characters from 'a' through 'z', which includes not being followed by any character." (?=[^a-z]) asserts "followed by a single character that is not any of the characters from 'a' through 'z'." The former is a negative assertion; the latter is a positive assertion.

      In your case, (?![a-z]) is what you would want to use.

      [PerlMonks posting tip: Enclose Perl code in <code></code> tags, even code within paragraphs.]

      UPDATE: Removed color.

        In your case,  (?![a-z]) is what you would want to use.

        One behavioral difference between these regexes and, in the case of the OP, the reason iaw4 would (probably) want to use this regex is that it can match at the end of a string and thus emulates the behavior of the  \b assertion. (Note:  \b can also match at the start of a string.)

        >perl -wMstrict -le "my $str = 'abcd'; for my $rx (qr{(?=[^a-z])}, qr{(?![a-z])}, qr{\b}) { my @offsets; push @offsets, $-[1] while $str =~ m{ ($rx) }xmsg; if (@offsets) { print qq{$rx matches '$str' at offset(s) @offsets}; } else { print qq{$rx does not match '$str'}; } } " (?-xism:(?=[^a-z])) does not match 'abcd' (?-xism:(?![a-z])) matches 'abcd' at offset(s) 4 (?-xism:\b) matches 'abcd' at offset(s) 0 4