diff --git a/Akash Articles/RegEx/Backreferencing.html b/Akash Articles/RegEx/Backreferencing.html index 9e926da..4487bf1 100644 --- a/Akash Articles/RegEx/Backreferencing.html +++ b/Akash Articles/RegEx/Backreferencing.html @@ -1,69 +1,131 @@ -## Backreferencing + + + + -Backreferencing is used to match same text again. Backreferences match the same text as previously matched by a capturing group. Let's look at an example: - -
- -
- -**Note:** `\/` is escaped `/`character, check it out in the appendix. - -The first captured group is (`\w+`), now we can use this group again by using a backreference (`\1`) at the closing tag, which matches the same text as in captured group `\w+`. - -You can backreference any captured group by using `\group_no`. - -Let's have two more examples: - -
- -
- -
- -
+ + + + + -### Backreferencing and character class +

Backreferencing

-Backreferencing can not be used in character class. Let's see an example: +

Backreferencing is used to match same text again. Backreferences match the same text as previously matched by a capturing group. Let's look at an example:

-
- -
+
+ +
-### Backreferencing and quantifiers +

Note: \/ is escaped /character, check it out in the appendix.

-When we are using a backreference for an expression with quantifiers, then we have to be careful. Let's observe it: +

The first captured group is (\w+), now we can use this group again by using a backreference (\1) at the closing tag, which matches the same text as in captured group \w+.

-
- -
+

You can backreference any captured group by using \group_no.

-Note that `(\d)+` and `(\d+)` both are different. So, what will happen for `(\d)+ -- \1` expression and same text above? +

Let's have two more examples:

-
- -
+
+ +
-Can you observe something? +
+ +
-For `(\d)+ -- \1` expression and `123 -- 3` string, first time 1 was stored in \1, then 2 was stored in \1 and at last 3 was stored. So, it will show a match if and only if the last character before ` --` is exactly same as the character after `-- `. +

Backreferencing and character class

-**Problems:** +

Backreferencing can not be used in character class. Let's see an example:

-1. Match any palindrome string of length 6, having only lowercase letters. - Answer: `([a-z])([a-z])([a-z])\3\2\1` +
+ +
-
- -
+

Backreferencing and quantifiers

-2. **RegEx**: `(\w+)oo\1le`
-**Text:** `google, doodle jump, ggooggle, ssoosle` +

When we are using a backreference for an expression with quantifiers, then we have to be careful. Let's observe it:

- Answer: -
- -
+
+ +
-**Note:** For group numbers more than 9, there is a syntax difference. +

Note that (\d)+ and (\d+) both are different. So, what will happen for (\d)+ -- \1 expression and same text above?

+ +
+ +
+ +

Can you observe something?

+ +

For (\d)+ -- \1 expression and 123 -- 3 string, first time 1 was stored in \1, then 2 was stored in \1 and at last 3 was stored. So, it will show a match if and only if the last character before -- is exactly same as the character after --.

+ +

Problems:

+ +
    +
  1. Match any palindrome string of length 6, having only lowercase letters. + Answer: ([a-z])([a-z])([a-z])\3\2\1

    + +
    + +
  2. + +
  3. RegEx: (\w+)oo\1le
    + Text: google, doodle jump, ggooggle, ssoosle

    + +

    Answer:

    + +
    + +
  4. +
+ +

Note: For group numbers more than 9, there is a syntax difference.

+ + + + +