Why those two expressions are not equal?

John Smith

I want to capture words that don't have the same letters next to each other. This one finds a letter and then checks if another letter next to it is not the same. This one works.

\b((\w)(?!(\2)))+\b

This one says do not match words that have the same letters next to each other, at least that's what I think it should do, but it doesn't work.

\b(?!((\w)\3))+\b
Borodin

The second pattern \b(?!((\w)\3))+\b is very odd

  • There is no \3 because the negative look-ahead (?!...) doesn't form a capture group, so you have just two groups

  • If the overall pattern matches then you know the negative look-ahead hasn't matched, so \1 and \2 will never be defined

  • You are asking for multiple copies of a zero-width pattern (a negative look-ahead matches a single point in a string)

  • The negative look-ahead will never consume any characters, so there can never be any characters between the two word boundaries

It's hard to know how to "fix" this for you as I can't see the algorithm that you were trying to implement. You first pattern doesn't need the parentheses around \2: just (?!\2) is fine, and you don't capture the whole word to use after the match, which is usually a better way to go than using the whole matched string

I think I would write this

\b((?:(\w)(?!\2))+)\b

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why aren't those two expressions semantically equivalent?

From Dev

Why aren't those two expressions semantically equivalent?

From Dev

Why are those two datetimes different?

From Dev

Why are these two strings not equal?

From Dev

Why these two are not equal?

From Dev

Javascript: Why are two objects not equal?

From Dev

Why are these two values not equal on arduino?

From Dev

Why are these two variables equal in JavaScript?

From Dev

Why aren't these two equal?

From Dev

Why those two task definition syntaxes equivalent?

From Dev

JavaScript Date: Why those two dates are different?

From Dev

Why does the 'is' operator behave unexpectedly with arithmetically equal expressions

From Dev

Why does the 'is' operator behave unexpectedly with arithmetically equal expressions

From Dev

In the theory of computation, why are regular expressions (ab)* and a*b* not equal?

From Dev

Why are my two python strings not equal in my program but equal in the interpreter?

From Dev

Why are some of these two proven-to-be-equal decimals in Python considered not to be equal?

From Dev

Two lists with same elements are not equal. Why?

From Dev

Why aren't these two arrays equal?

From Dev

Why are two different numbers equal in JavaScript?

From Dev

Golang Why aren't these two strings equal?

From Dev

Why aren't these two byte operations equal?

From Dev

Why are these two function calls not equal to each other?

From Dev

Why are two equivalent ISODate values not equal?

From Dev

Why are these two identical strings not equal in JavaScript?

From Dev

why these two java.util.Pattern are not equal

From Dev

Erlang - Why do these two expressions not match?

From Dev

Why those two std::string find() functions are declared as noexcept?

From Dev

Why those two sed commands get different result?

From Dev

Why are those weird results calculating years between two JODA instants?

Related Related

  1. 1

    Why aren't those two expressions semantically equivalent?

  2. 2

    Why aren't those two expressions semantically equivalent?

  3. 3

    Why are those two datetimes different?

  4. 4

    Why are these two strings not equal?

  5. 5

    Why these two are not equal?

  6. 6

    Javascript: Why are two objects not equal?

  7. 7

    Why are these two values not equal on arduino?

  8. 8

    Why are these two variables equal in JavaScript?

  9. 9

    Why aren't these two equal?

  10. 10

    Why those two task definition syntaxes equivalent?

  11. 11

    JavaScript Date: Why those two dates are different?

  12. 12

    Why does the 'is' operator behave unexpectedly with arithmetically equal expressions

  13. 13

    Why does the 'is' operator behave unexpectedly with arithmetically equal expressions

  14. 14

    In the theory of computation, why are regular expressions (ab)* and a*b* not equal?

  15. 15

    Why are my two python strings not equal in my program but equal in the interpreter?

  16. 16

    Why are some of these two proven-to-be-equal decimals in Python considered not to be equal?

  17. 17

    Two lists with same elements are not equal. Why?

  18. 18

    Why aren't these two arrays equal?

  19. 19

    Why are two different numbers equal in JavaScript?

  20. 20

    Golang Why aren't these two strings equal?

  21. 21

    Why aren't these two byte operations equal?

  22. 22

    Why are these two function calls not equal to each other?

  23. 23

    Why are two equivalent ISODate values not equal?

  24. 24

    Why are these two identical strings not equal in JavaScript?

  25. 25

    why these two java.util.Pattern are not equal

  26. 26

    Erlang - Why do these two expressions not match?

  27. 27

    Why those two std::string find() functions are declared as noexcept?

  28. 28

    Why those two sed commands get different result?

  29. 29

    Why are those weird results calculating years between two JODA instants?

HotTag

Archive