Header Ads Widget

Ticker

6/random

Python RegEx

Python RegEx


In Python, regular expressions (regex or regexp) are a powerful tool for pattern matching and manipulation of strings. The `re` module provides support for regular expressions. 


Here's an explanation with examples:


1. Basic Pattern Matching:


```python

import re


pattern = r"apple"

text = "I love apples and oranges."


match = re.search(pattern, text)


if match:

    print("Found a match!")

else:

    print("No match found.")

```


In this example, the `search` function is used to find the first occurrence of the pattern "apple" in the given text.


2. Matching Multiple Occurrences:


```python

import re


pattern = r"cat"

text = "The cat and the hat sat on the mat."


matches = re.findall(pattern, text)


print(matches)

```


The `findall` function is used to find all occurrences of the pattern "cat" in the given text.


3. Matching Any Character:


```python

import re


pattern = r".at"

text = "cat, hat, rat, bat"


matches = re.findall(pattern, text)


print(matches)

```


The dot (`.`) in the pattern matches any character, so this would match words ending with "at."


4. Character Classes:


```python

import re


pattern = r"[aeiou]"

text = "Hello, World!"


vowels = re.findall(pattern, text)


print(vowels)

```


The character class `[aeiou]` matches any vowel in the given text.


5. Quantifiers:


```python

import re


pattern = r"\d{3}-\d{2}-\d{4}"

text = "My SSN is 123-45-6789."


ssn = re.search(pattern, text)


if ssn:

    print(f"Found SSN: {ssn.group()}")

else:

    print("No SSN found.")

```


The `\d{3}-\d{2}-\d{4}` pattern matches a Social Security Number (SSN) format.


6. Groups and Capturing:


```python

import re


pattern = r"(\d{3})-(\d{2})-(\d{4})"

text = "My SSN is 123-45-6789."


ssn_match = re.search(pattern, text)


if ssn_match:

    print("Found SSN:")

    print(f"Full SSN: {ssn_match.group()}")

    print(f"Group 1: {ssn_match.group(1)}")

    print(f"Group 2: {ssn_match.group(2)}")

    print(f"Group 3: {ssn_match.group(3)}")

else:

    print("No SSN found.")

```


Parentheses `()` are used to create groups. The `group` method is used to access the matched groups.


7. Anchors:


```python

import re


pattern = r"\bword\b"

text = "This is a word. Not just a keyword."


matches = re.findall(pattern, text)


print(matches)

```


The `\b` anchors represent word boundaries, so this matches the standalone word "word."


Regular expressions in Python provide a powerful and flexible way to perform string matching and manipulation. They are widely used in tasks such as text parsing, data validation, and searching.

Post a Comment

0 Comments