Advertisement
mikhail_dvorkin

RegExp draft

Dec 5th, 2016
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.72 KB | None | 0 0
  1. >>> import re
  2. >>> re.match("\\w{5,20}", "v.pupkin")
  3. >>> len("\\\\")
  4. 2
  5. >>> re.match("\\\\", "\\")
  6. <_sre.SRE_Match object; span=(0, 1), match='\\'>
  7. >>> bool(re.match("\\\\", "\\"))
  8. True
  9. >>> "\\\\"
  10. '\\\\'
  11. >>> print("\\\\")
  12. \\
  13. >>> re.match("\\\\", "\\\\")
  14. <_sre.SRE_Match object; span=(0, 1), match='\\'>
  15. >>> bool(re.match("\\\\", "\\\\"))
  16. True
  17. >>> bool(re.match("^\\\\$", "\\\\"))
  18. False
  19. >>> bool(re.match("^\\\\$", "\\"))
  20. True
  21. >>> bool(re.match("\\\\", "\\\\"))
  22. True
  23. >>> bool(re.match("^\\\\$", "\\"))
  24. True
  25. >>> bool(re.match("^\\\\$", "\\\\"))
  26. False
  27. >>> bool(re.match("\\\\", "\\\\\\\\\\\\"))
  28. True
  29. >>> bool(re.match("devil", "He is a devil"))
  30. False
  31. >>> bool(re.match(".*devil.*", "He is a devil"))
  32. True
  33. >>> bool(re.match(".*devil", "He is a devil"))
  34. True
  35. >>> bool(re.match("devil", "He is a devil"))
  36. False
  37. >>> bool(re.match("devil", "He is a devil "))
  38. False
  39. >>> bool(re.match("devil", "devil "))
  40. True
  41. >>> help(re.match)
  42.  
  43. >>> bool(re.fullmatch("\\w{5,20}", "v.pupkin"))
  44. False
  45. >>> bool(re.fullmatch("\\w{5,20}", "v_pupkin"))
  46. True
  47. >>> print("\\w{5,20}")
  48. \w{5,20}
  49. >>> bool(re.fullmatch(".*devil.*", "He is a devil."))
  50. True
  51. >>> bool(re.fullmatch(".*(devil|hell|daemon).*", "He is a devil."))
  52. True
  53. >>> bool(re.fullmatch(".*devil|hell|daemon.*", "He is a devil."))
  54. False
  55. >>> bool(re.fullmatch(".*(devil|hell|daemon).*", "He is a devil."))
  56. True
  57. >>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.")
  58. <_sre.SRE_Match object; span=(0, 14), match='He is a devil.'>
  59. >>> if re.fullmatch(".*(devil|hell|daemon).*", "He is a devil."):
  60. ...     print("Shut up")
  61. ...
  62. Shut up
  63. >>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.")
  64. <_sre.SRE_Match object; span=(0, 14), match='He is a devil.'>
  65. >>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.").groups()
  66. ('devil',)
  67. >>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.").group(1)
  68. 'devil'
  69. >>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil.").groups()
  70. ('devil',)
  71. >>> re.fullmatch(".*(devil|hell|daemon).*", "Go to hell. He is a devil.").groups()
  72. ('devil',)
  73. >>> re.fullmatch(".*(devil|hell|daemon).*", "Go to hell. He is a devil. Go to hell.").groups()
  74. ('hell',)
  75. >>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil. Go to hell.").groups()
  76. ('hell',)
  77. >>> re.fullmatch("a(b*)c(d*)", "abbc").groups()
  78. ('bb', '')
  79. >>> re.fullmatch("(\\S+)@(\\S+)", "mikhail.dvorkin@gmail.com").groups()
  80. ('mikhail.dvorkin', 'gmail.com')
  81. >>> re.fullmatch(".*(devil|hell|daemon).*", "He is a devil. Go to hell.").groups()
  82. ('hell',)
  83. >>> re.search("devil|hell|daemon", "He is a devil. Go to hell.")
  84. <_sre.SRE_Match object; span=(8, 13), match='devil'>
  85. >>> re.search("devil|hell|daemon", "He is a devil. Go to hell.").groups()
  86. ()
  87. >>> re.search("devil|hell|daemon", "He is a devil. Go to hell.")
  88. <_sre.SRE_Match object; span=(8, 13), match='devil'>
  89. >>> list(re.search("devil|hell|daemon", "He is a devil. Go to hell."))
  90. Traceback (most recent call last):
  91.   File "<stdin>", line 1, in <module>
  92. TypeError: '_sre.SRE_Match' object is not iterable
  93. >>> dir(re.search("devil|hell|daemon", "He is a devil. Go to hell."))
  94. ['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'end', 'endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']
  95. >>> re.search("devil|hell|daemon", "He is a devil. Go to hell.").groups()
  96. ()
  97. >>> re.search("(devil|hell|daemon)", "He is a devil. Go to hell.").groups()
  98. ('devil',)
  99. >>> re.findall("(devil|hell|daemon)", "He is a devil. Go to hell.").groups()
  100. Traceback (most recent call last):
  101.   File "<stdin>", line 1, in <module>
  102. AttributeError: 'list' object has no attribute 'groups'
  103. >>> re.findall("(devil|hell|daemon)", "He is a devil. Go to hell.")
  104. ['devil', 'hell']
  105. >>> re.findall("devil|hell|daemon", "He is a devil. Go to hell.")
  106. ['devil', 'hell']
  107. >>> len(re.findall("devil|hell|daemon", "He is a devil. Go to hell."))
  108. 2
  109. >>> len(re.findall("devil|hell|daemon", "He is a devil. Go to hell."))
  110. 2
  111. >>> re.sub("devil|hell|daemon", "He is a devil. Go to hell.", "***")
  112. '***'
  113. >>> re.sub("devil|hell|daemon", "***", "He is a devil. Go to hell.")
  114. 'He is a ***. Go to ***.'
  115. >>> re.search("devil|hell|daemon", "He is a devil. Go to hell.")
  116. <_sre.SRE_Match object; span=(8, 13), match='devil'>
  117. >>> bool(re.search("devil|hell|daemon", "He is a devil. Go to hell."))
  118. True
  119. >>> re.search("devil|hell|daemon", "He is a devil. Go to hell.").groups()
  120. ()
  121. >>> re.search("(devil|hell|daemon)", "He is a devil. Go to hell.").groups()
  122. ('devil',)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement