473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

\1 in regular expression

can anyone tell me usage of (\1) in regex??

Test String: <font size=10><span>label<span></font>
Pattern: <font[^>]*>(.*?)\s*\w+\s*(\1)</font>
This is working

but if i change latter <span> to </span>and try to write pattern with (/1),am not getting it.

Pls give the solution

Thank you
Jan 10 '08 #1
3 1439
bvdet
2,851 Expert Mod 2GB
can anyone tell me usage of (\1) in regex??

Test String: <font size=10><span>label<span></font>
Pattern: <font[^>]*>(.*?)\s*\w+\s*(\1)</font>
This is working

but if i change latter <span> to </span>and try to write pattern with (/1),am not getting it.

Pls give the solution

Thank you
The matched substring will be saved when matched to an expression enclosed in parentheses and can be accessed with the group(group number) method of match objects. The '\1' in your expression matches the text that was matched by group number 1. Since it is in parentheses, it also will be saved. This may give you some ideas:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. p = re.compile(r'<font[^>]*><?(.*?)>?\s*(\w+)\s*<?/?(\1)>?</font>')
  4.  
  5. s = '<font size=10><span>label</span></font>'
  6.  
  7. m = p.match(s)
  8. print m.group(0)
  9. print m.groups()
  10.  
  11. >>> <font size=10><span>label</span></font>
  12. ('span', 'label', 'span')
  13. >>> m.group(1)
  14. 'span'
  15. >>> m.group(2)
  16. 'label'
  17. >>> m.group(3)
  18. 'span'
  19. >>> 
Same, but add another group to the expression:
Expand|Select|Wrap|Line Numbers
  1. p1 = re.compile(r'<font\s*([^>]*)><?(.*?)>?\s*(\w+)\s*<?/?(\2)>?</font>')
  2. s1 = '<font size=10><span>label</span></font>'
  3. m1 = p1.match(s1)
  4. print m1.group(0)
  5. print m1.groups()
  6.  
  7. '''
  8. >>> <font size=10><span>label</span></font>
  9. ('size=10', 'span', 'label', 'span')
  10. >>>
  11. '''
  12.  
  13. s2 = '<font><span>label</span></font>'
  14. m2 = p1.match(s2)
  15. print m2.group(0)
  16. print m2.groups()
  17.  
  18. '''
  19. >>> <font><span>label</span></font>
  20. ('', 'span', 'label', 'span')
  21. >>>
  22. '''
Jan 10 '08 #2
Thank u bv...
i understood concept but a small doubt..

in <font[^>]*><?(.*?)>?\s*(\w+)\s*<?/?(\1)>?</font>...y have u used '?' before every expresion?i didnt find differen in its absence...
Jan 11 '08 #3
bvdet
2,851 Expert Mod 2GB
Thank u bv...
i understood concept but a small doubt..

in <font[^>]*><?(.*?)>?\s*(\w+)\s*<?/?(\1)>?</font>...y have u used '?' before every expresion?i didnt find differen in its absence...
The '?' character by itself matches 0 repetitions or 1 repetition of the expression that precedes it. Therefore the match is optional.
Jan 11 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.