Regex defined zero-width assertions

Regex defined zero-width assertions use ( and ) and there is a ? after the opening (. There are two assertions: positive, denoted by the = character, and negative, denoted by the ! character. If the assertion is backward-looking then the ? character is followed by a < character. Thus, (?=...) is a positive lookahead assertion and (?<!...) is a negative lookbehind assertion.

(?=pattern)

The positive lookahead assertion ensures that the string matches the pattern after the current position. For example, abc(?=K) ensures that the characters "abc" in the checked strings are followed by the letter "K", but this check does not consume the character "K".

(?!pattern)

The negative lookahead assertion ensures that the string does not match the pattern after the current position. For example, abc(?!Z) ensures that the characters "abc" in the checked string are not followed by the letter "Z", but this check does not consume the character "Z".

(?<=pattern)

The positive lookbehind assertion ensures that the string matches the pattern before the current position. For example, (?<=P)abc ensures that the characters "abc" in the checked string are preceded by the letter "P", but this check does not consume the character "P".

(?<!pattern)

The negative lookbehind assertion ensures that the string does not match the pattern before the current position. For example, (?<!Q)abc ensures that the characters "abc" in the checked string are not preceded by the letter "Q", but this check does not consume the character "Q".

 

In the next section, we will look into more details of the G boundary assertions, and then, you will learn about the lookahead and lookbehind assertions. However, before the lookarounds, we will discuss atomic groups, which are an important construct and topic to ease the understanding of the behavior of the lookahead and lookbehind assertions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.12.34