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: - -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 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` +When we are using a backreference for an expression with quantifiers, then we have to be careful. Let's observe it:
- Answer: -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:
+ +Match any palindrome string of length 6, having only lowercase letters.
+ Answer: ([a-z])([a-z])([a-z])\3\2\1
RegEx: (\w+)oo\1le
+ Text: google, doodle jump, ggooggle, ssoosle
Answer:
+ +Note: For group numbers more than 9, there is a syntax difference.
+ + + + +