alex_bn_lee

导航

[1115] Python re library examples

Ref: Python RegEx


To find a pattern like "Page " followed by a number in a string, you can use the `re` (regular expressions) module in Python. Here's how you can do it:
  1. Import the re module:

    import re
    
  2. Define your string:

    text = "This is a sample text with Page 1, Page 23, and Page 456."
    
  3. Use a regular expression to find the pattern:

    # Define the pattern
    pattern = r"Page \d+"
    
    # Find all matches
    matches = re.findall(pattern, text)
    
    # Print the matches
    print(matches)
    

Explanation:

  • import re: Imports the re module for working with regular expressions.
  • pattern = r"Page \d+": Defines the pattern to search for. Page matches the literal text "Page ", and \d+ matches one or more digits.
  • re.findall(pattern, text): Finds all occurrences of the pattern in the string.
  • print(matches): Prints the list of matches.

Example:

Here's a complete example:

import re

# Define your string
text = "This is a sample text with Page 1, Page 23, and Page 456."

# Define the pattern
pattern = r"Page \d+"

# Find all matches
matches = re.findall(pattern, text)

# Print the matches
print(matches)

Output:

Running this script will produce the following output:

['Page 1', 'Page 23', 'Page 456']

This will give you a list of all occurrences of "Page " followed by a number in the string.

If you need any further assistance or have more questions, just let me know! 😊


To get the index of the first match for a pattern in a string using the re module in Python, you can use the re.search() function. Here's how you can do it:

  1. Import the re module:

    import re
    
  2. Define your string:

    text = "This is a sample text with Page 1, Page 23, and Page 456."
    
  3. Use a regular expression to find the pattern and get the index:

    # Define the pattern
    pattern = r"Page \d+"
    
    # Search for the pattern in the text
    match = re.search(pattern, text)
    
    # Check if a match is found
    if match:
        # Get the index of the first match
        index = match.start()
        print(f"First match found at index {index}")
    else:
        print("No match found")
    

Explanation:

  • import re: Imports the re module for working with regular expressions.
  • pattern = r"Page \d+": Defines the pattern to search for. Page matches the literal text "Page ", and \d+ matches one or more digits.
  • re.search(pattern, text): Searches for the pattern in the text. It returns a match object if the pattern is found; otherwise, it returns None.
  • match.start(): Returns the starting index of the match.

Example:

Here's a complete example:

import re

# Define your string
text = "This is a sample text with Page 1, Page 23, and Page 456."

# Define the pattern
pattern = r"Page \d+"

# Search for the pattern in the text
match = re.search(pattern, text)

# Check if a match is found
if match:
    # Get the index of the first match
    index = match.start()
    print(f"First match found at index {index}")
else:
    print("No match found")

Output:

Running this script will produce the following output:

First match found at index 26

This will give you the index of the first occurrence of "Page " followed by a number in the string.

If you need any further assistance or have more questions, just let me know! 😊

posted on 2025-02-27 08:29  McDelfino  阅读(31)  评论(0)    收藏  举报