How to Use Python Regular Expressions in Java

Source: Internet
Author: User

When using Python regular expressions, you need to pay attention to many issues. For Python regular expressions, such as continuous learning, can be better solved. Next we will look at how we can better learn. I hope you will have some gains.

Java code

 
 
  1. # Encoding = UTF-8
  2. '''
  3. Python learn regular express
  4. Url: http://docs.python.org/library/re.html
  5. Parse html url: http://www.boddie.org.uk/python/HTML.html
  6. Author: liuzheng
  7. '''
  8. Import re
  9. Import urllib
  10. # Analyze the javaeye blog Channel
  11. Class ParseHTML:
  12. '''
  13. Parse html for infomation
  14. Parse javeeye page
  15. '''
  16. Def _ init _ (self, url ):
  17. Self. url = url
  18. Pass
  19. # Analyses html
  20. Def parse (self ):
  21. Sock = urllib. urlopen (self. url)
  22. Html = sock. read ()
  23. Self. _ puts (html)
  24. Pass
  25. # Print html matching data
  26. Def _ puts (self, html ):
  27. B = re. compile (r "<a href = '([\ w./: \] + ?) '[\ S] * title = ([^ <>] + ?)
    [\ S] * target = ([^ <>] +?)> ([^ <>] + ?) </A> ", re. I)
  28. M = re. findall (B, html)
  29. # Is there an encode problem ?, I don't know. Can you help me?
  30. Print m
  31. If _ name _ = '_ main __':
  32. Url = "http://www.javaeye.com/blogs"
  33. P = ParseHTML (url)
  34. P. parse ()
  35. If _ debug __:
  36. Print "debuging is % s" % _ debug __
  37. Print "regular" + "*" * 30
  38. # Math
  39. Str = "800-820-8800"
  40. M = re. match (r "(\ d {3})-(\ d {3})-(\ d {4})", str)
  41. Print "result:", m. groups ()
  42. # Split
  43. Print "split: % s" % re. split ('\ W', 'words, Words, words .')
  44. # Findall
  45. Text = "He was carefully disguised but captured quickly
    By police ."
  46. Print "findall: % s" % re. findall (r "\ w + ly", text)
  47. # Sub
  48. Text = "hello world! "
  49. Print "sub: % s" % re. sub (r "\ s +", "--", text)

The above is a detailed introduction to Python regular expressions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.