Advertisement
go6odn28

lookahead

Dec 19th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. # ################### Python Positive Lookahead ###########################
  2.  
  3. # So, in this case, the regular expression is looking
  4. # for word boundaries (\b), followed by one or more word characters (\w+),
  5. # but only if they are immediately followed by the substring "ing" ((?=ing)).
  6. # The result is that it finds and returns the word "cod" from the word
  7. # "coding" in the input text.
  8.  
  9. import re
  10. text = "I love coding in Python!"
  11. pattern = r"\b\w+(?=ing)"
  12. result = re.findall(pattern, text)
  13. print(result)
  14. # Output: ['cod']
  15. print()
  16.  
  17.  
  18. text_2 = "I love coding in Python!"
  19. pattern_2 = r"(\b\w+\s*)+(?=ing)"
  20. result_2 = re.search(pattern_2, text_2)
  21. if result_2:
  22.     print(result_2.group())
  23. # Output: I love cod
  24. print()
  25.  
  26. text_3 = "Ilovecoding in Python!"
  27. pattern_3 = r"(\b\w+\s*)*(?=ing)"
  28. result_3 = re.search(pattern_2, text_3)
  29. if result_3:
  30.     print(result_3.group())
  31. # Output: Ilovecod
  32. print()
  33.  
  34.  
  35. # In this example, the pattern looks for words that are followed by a space
  36. # and the word "and". The result is the word "bright" because it fulfills
  37. # the criteria specified by the positive lookahead assertion.
  38.  
  39. text_4 = "The future is bright and coding is exciting!"
  40. pattern_4 = r"\b\w+(?=\sand)"
  41. result_4 = re.findall(pattern_4, text_4)
  42. print(result_4)
  43. # Output: ['bright']
  44. print()
  45.  
  46. text_5 = "Regular expressions are powerful and Python is awesome!"
  47. pattern_5 = r"\b\w+(?=\sand)"
  48. result_5 = re.findall(pattern_5, text_5)
  49. print(result_5)
  50. # ['powerful']
  51. print()
  52.  
  53. text_6 = "The future is bright and coding is exciting!"
  54. pattern_6 = r"\b\w+(?=\sand)"
  55. result_6 = re.findall(pattern_6, text_6)
  56. print(result_6)
  57. # ['bright']
  58. print()
  59.  
  60. text_7 = "Positive thinking leads to positive outcomes."
  61. pattern_7 = r"\b\w+(?=\sthinking)"
  62. result_7 = re.findall(pattern_7, text_7)
  63. print(result_7)
  64. # ['Positive']
  65. print()
  66.  
  67. text_8 = "1 Python is about 4 feet long"
  68. pattern_8 = r"\d+(?=\s*feet)"
  69. result_8 = re.findall(pattern_8, text_8)
  70. print(*result_8)
  71. # 4
  72. print()
  73.  
  74.  
  75. #  ################### Python Negative Lookahead ###########################
  76. print("                         Negative Lookahead                      ")
  77.  
  78. # Example:
  79. # X(?!Y)
  80.  
  81. # The X(?!Y) matches X only if it is not followed by Y. It’s the \d+ not followed
  82. # by the literal string feet.
  83.  
  84.  
  85. text_9 = '1 Python is about 4 feet long'
  86. pattern_9 = '\d+(?!\s*feet)'
  87.  
  88. matches = re.finditer(pattern_9, text_9)
  89. for match in matches:
  90.     print(match.group())
  91.  
  92. # 1
  93. print()
  94.  
  95. # positive multiply
  96. print("positive multiply")
  97. text_10 = "Python3 Tutorial4B"
  98. pattern_10 = r'\b\w+(?=\d)(?=[A-Z])'
  99.  
  100. result_10 = re.findall(pattern_10, text_10)
  101. print(result_10)
  102. # []
  103. print()
  104.  
  105.  
  106. text_11 = "Python3 Tutorial4B"
  107. pattern_11 = r'\b\w+(?=\d)(?=[A-Z]*)'
  108.  
  109. result_11 = re.findall(pattern_11, text_11)
  110. print(result_11)
  111. # ['Python', 'Tutorial']
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement