Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression
No Match

r"
"
gm

Test String

Substitution

Processing...

Code Generator

Generated Code

#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox Local $sRegex = "(?m)\#(.*?)\*\*(.*?)\*\*" Local $sString = "Here’s an extended version of the previous explanation, now including **problem definitions**, **approaches**, and **examples** for each interval problem:" & @CRLF & _ "" & @CRLF & _ "---" & @CRLF & _ "" & @CRLF & _ "### 1. **Basic Interval Problems**" & @CRLF & _ "" & @CRLF & _ "#### a) **Finding Overlaps Between Two Intervals**" & @CRLF & _ "" & @CRLF & _ "- **Problem Definition**: Given two intervals, determine if they overlap. Two intervals overlap if they share at least one point in common." & @CRLF & _ " " & @CRLF & _ "- **Approach**: " & @CRLF & _ " - Check if the start of one interval is before the end of the other and vice versa." & @CRLF & _ " " & @CRLF & _ "- **Example**: " & @CRLF & _ " - Interval A = [1, 5], Interval B = [4, 9]." & @CRLF & _ " - Output: True (they overlap)." & @CRLF & _ "" & @CRLF & _ "```python" & @CRLF & _ "def do_intervals_overlap(interval1, interval2):" & @CRLF & _ " return interval1[0] <= interval2[1] and interval2[0] <= interval1[1]" & @CRLF & _ "" & @CRLF & _ "# Example" & @CRLF & _ "interval1 = [1, 5]" & @CRLF & _ "interval2 = [4, 9]" & @CRLF & _ "print(do_intervals_overlap(interval1, interval2)) # True" & @CRLF & _ "```" & @CRLF & _ "" & @CRLF & _ "#### b) **Union of Two Intervals**" & @CRLF & _ "" & @CRLF & _ "- **Problem Definition**: Given two intervals, return the union of the intervals if they overlap or are adjacent. If they do not overlap, return both intervals." & @CRLF & _ "" & @CRLF & _ "- **Approach**: " & @CRLF & _ " - If the intervals overlap, return the minimum start and maximum end of the two intervals." & @CRLF & _ "" & @CRLF & _ "- **Example**: " & @CRLF & _ " - Interval A = [1, 5], Interval B = [4, 9]." & @CRLF & _ " - Output: [1, 9]." & @CRLF & _ "" & @CRLF & _ "```python" & @CRLF & _ "def union_intervals(interval1, interval2):" & @CRLF & _ " if do_intervals_overlap(interval1, interval2):" & @CRLF & _ " return [min(interval1[0], interval2[0]), max(interval1[1], interval2[1])]" & @CRLF & _ " return [interval1, interval2]" & @CRLF & _ "" & @CRLF & _ "# Example" & @CRLF & _ "print(union_intervals([1, 5], [4, 9])) # [1, 9]" & @CRLF & _ "```" & @CRLF & _ "" & @CRLF & _ "#### c) **Intersection of Two Intervals**" & @CRLF & _ "" & @CRLF & _ "- **Problem Definition**: Find the intersection of two intervals. The intersection is the range that is covered by both intervals." & @CRLF & _ "" & @CRLF & _ "- **Approach**: " & @CRLF & _ " - If the intervals overlap, the intersection is the maximum of the start points and the minimum of the end points." & @CRLF & _ "" & @CRLF & _ "- **Example**: " & @CRLF & _ " - Interval A = [1, 5], Interval B = [3, 9]." & @CRLF & _ " - Output: [3, 5]." & @CRLF & _ "" & @CRLF & _ "```python" & @CRLF & _ "def intersection_intervals(interval1, interval2):" & @CRLF & _ " if do_intervals_overlap(interval1, interval2):" & @CRLF & _ " return [max(interval1[0], interval2[0]), min(interval1[1], interval2[1])]" & @CRLF & _ " return None" & @CRLF & _ "" & @CRLF & _ "# Example" & @CRLF & _ "print(intersection_intervals([1, 5], [3, 9])) # [3, 5]" & @CRLF & _ "```" & @CRLF & _ "" & @CRLF & _ "---" & @CRLF & _ "" & @CRLF & _ "### 2. **Multiple Intervals Problems**" & @CRLF & _ "" & @CRLF & _ "#### a) **Merging Overlapping Intervals**" & @CRLF & _ "" & @CRLF & _ "- **Problem Definition**: Given a set of intervals, merge all the overlapping ones." & @CRLF & _ "" & @CRLF & _ "- **Approach**: " & @CRLF & _ " - Sort the intervals by their start time, then iterate through them and merge overlapping intervals." & @CRLF & _ "" & @CRLF & _ "- **Example**: " & @CRLF & _ " - Intervals = [[1, 5], [2, 6], [8, 10]]." & @CRLF & _ " - Output: [[1, 6], [8, 10]]." & @CRLF & _ "" & @CRLF & _ "```python" & @CRLF & _ "def merge_intervals(intervals):" & @CRLF & _ " intervals.sort(key=lambda x: x[0])" & @CRLF & _ " merged = [intervals[0]]" & @CRLF & _ " " & @CRLF & _ " for i in range(1, len(intervals)):" & @CRLF & _ " if merged[-1][1] >= intervals[i][0]:" & @CRLF & _ " merged[-1][1] = max(merged[-1][1], intervals[i][1])" & @CRLF & _ " else:" & @CRLF & _ " merged.append(intervals[i])" & @CRLF & _ " " & @CRLF & _ " return merged" & @CRLF & _ "" & @CRLF & _ "# Example" & @CRLF & _ "print(merge_intervals([[1, 5], [2, 6], [8, 10]])) # [[1, 6], [8, 10]]" & @CRLF & _ "```" & @CRLF & _ "" & @CRLF & _ "#### b) **Finding Gaps Between Intervals**" & @CRLF & _ "" & @CRLF & _ "- **Problem Definition**: Given a set of intervals, find the gaps where no interval exists." & @CRLF & _ "" & @CRLF & _ "- **Approach**: " & @CRLF & _ " - Sort intervals by start time. Identify gaps by checking the end of one interval and the start of the next." & @CRLF & _ "" & @CRLF & _ "- **Example**: " & @CRLF & _ " - Intervals = [[1, 5], [7, 10]]." & @CRLF & _ " - Output: [5, 7]." & @CRLF & _ "" & @CRLF & _ "```python" & @CRLF & _ "def find_gaps(intervals):" & @CRLF & _ " intervals.sort(key=lambda x: x[0])" & @CRLF & _ " gaps = []" & @CRLF & _ " " & @CRLF & _ " for i in range(1, len(intervals)):" & @CRLF & _ " if intervals[i][0] > intervals[i-1][1]:" & @CRLF & _ " gaps.append([intervals[i-1][1], intervals[i][0]])" & @CRLF & _ " " & @CRLF & _ " return gaps" & @CRLF & _ "" & @CRLF & _ "# Example" & @CRLF & _ "print(find_gaps([[1, 5], [7, 10]])) # [[5, 7]]" & @CRLF & _ "```" & @CRLF & _ "" & @CRLF & _ "---" & @CRLF & _ "" & @CRLF & _ "### 3. **Complex Interval Operations**" & @CRLF & _ "" & @CRLF & _ "#### a) **Interval Difference**" & @CRLF & _ "" & @CRLF & _ "- **Problem Definition**: Given two intervals, find the difference between them, i.e., the part of the first interval that does not overlap with the second." & @CRLF & _ "" & @CRLF & _ "- **Approach**: " & @CRLF & _ " - If the intervals overlap, return the non-overlapping portions of the first interval." & @CRLF & _ "" & @CRLF & _ "- **Example**: " & @CRLF & _ " - Interval A = [1, 10], Interval B = [5, 7]." & @CRLF & _ " - Output: [[1, 5], [7, 10]]." & @CRLF & _ "" & @CRLF & _ "```python" & @CRLF & _ "def interval_difference(A, B):" & @CRLF & _ " if not do_intervals_overlap(A, B):" & @CRLF & _ " return [A] # No overlap" & @CRLF & _ " " & @CRLF & _ " result = []" & @CRLF & _ " if A[0] < B[0]:" & @CRLF & _ " result.append([A[0], B[0]])" & @CRLF & _ " if A[1] > B[1]:" & @CRLF & _ " result.append([B[1], A[1]])" & @CRLF & _ " " & @CRLF & _ " return result" & @CRLF & _ "" & @CRLF & _ "# Example" & @CRLF & _ "print(interval_difference([1, 10], [5, 7])) # [[1, 5], [7, 10]]" & @CRLF & _ "```" & @CRLF & _ "" & @CRLF & _ "#### b) **Interval Scheduling**" & @CRLF & _ "" & @CRLF & _ "- **Problem Definition**: Given a set of intervals, find the maximum number of non-overlapping intervals that can be selected." & @CRLF & _ "" & @CRLF & _ "- **Approach**: " & @CRLF & _ " - Sort intervals by end time. Greedily select intervals that do not overlap with the previously selected one." & @CRLF & _ "" & @CRLF & _ "- **Example**: " & @CRLF & _ " - Intervals = [[1, 3], [2, 4], [3, 5]]." & @CRLF & _ " - Output: [[1, 3], [3, 5]]." & @CRLF & _ "" & @CRLF & _ "```python" & @CRLF & _ "def interval_scheduling(intervals):" & @CRLF & _ " intervals.sort(key=lambda x: x[1]) # Sort by end times" & @CRLF & _ " result = []" & @CRLF & _ " last_end = float('-inf')" & @CRLF & _ " " & @CRLF & _ " for interval in intervals:" & @CRLF & _ " if interval[0] >= last_end:" & @CRLF & _ " result.append(interval)" & @CRLF & _ " last_end = interval[1]" & @CRLF & _ " " & @CRLF & _ " return result" & @CRLF & _ "" & @CRLF & _ "# Example" & @CRLF & _ "print(interval_scheduling([[1, 3], [2, 4], [3, 5]])) # [[1, 3], [3, 5]]" & @CRLF & _ "```" & @CRLF & _ "" & @CRLF & _ "---" & @CRLF & _ "" & @CRLF & _ "### 4. **Advanced Interval Challenges**" & @CRLF & _ "" & @CRLF & _ "#### a) **Finding the Smallest Range Covering All Points**" & @CRLF & _ "" & @CRLF & _ "- **Problem Definition**: Given several lists of intervals, find the smallest range that includes at least one interval from each list." & @CRLF & _ "" & @CRLF & _ "- **Approach**: " & @CRLF & _ " - Use a sliding window and min-heap to track the smallest range covering all lists." & @CRLF & _ "" & @CRLF & _ "- **Example**: " & @CRLF & _ " - Lists = [[1, 5], [6, 9], [7, 10]]." & @CRLF & _ " - Output: (6, 7)." & @CRLF & _ "" & @CRLF & _ "```python" & @CRLF & _ "import heapq" & @CRLF & _ "" & @CRLF & _ "def smallest_range(lists):" & @CRLF & _ " min_heap = []" & @CRLF & _ " max_val = float('-inf')" & @CRLF & _ " " & @CRLF & _ " # Add the first element of each list to the heap" & @CRLF & _ " for i, l in enumerate(lists):" & @CRLF & _ " heapq.heappush(min_heap, (l[0], i, 0))" & @CRLF & _ " max_val = max(max_val, l[0])" & @CRLF & _ " " & @CRLF & _ " min_range = float('inf'), None, None" & @CRLF & _ " " & @CRLF & _ " while min_heap:" & @CRLF & _ " min_val, list_idx, element_idx = heapq.heappop(min_heap)" & @CRLF & _ " if max_val - min_val < min_range[0]:" & @CRLF & _ " min_range = (max_val - min_val, min_val, max_val)" & @CRLF & _ " " & @CRLF & _ " if element_idx + 1 == len(lists[list_idx]):" & @CRLF & _ " break # We've reached the end of one list" & @CRLF & _ " " & @CRLF & _ " next_val = lists[list_idx][element_idx + 1]" & @CRLF & _ " max_val = max(max_val, next_val)" & @CRLF & _ " heapq.heappush(min_heap, (next_val, list_idx, element_idx + 1))" & @CRLF & _ " " & @CRLF & _ " return min_range[1], min_range[2]" & @CRLF & _ "" & @CRLF & _ "# Example" & @CRLF & _ "lists = [[1, 5], [6, 9], [7, 10]]" & @CRLF & _ "print(smallest_range(lists)) # (6, 7)" & @CRLF & _ "```" & @CRLF & _ "" & @CRLF & _ "#### b) **K-Interval Coverage**" & @CRLF & _ "" & @CRLF & _ "- **Problem Definition**: Given a set of intervals and an integer `k`, find the maximum number of intervals that cover any point or range." & @CRLF & _ "" & @CRLF & _ "- **Approach**: " & @CRLF & _ " - Sort events (start and end of intervals), track the number of overlapping intervals at each event, and find the maximum count." & @CRLF & _ "" & @CRLF & _ "- **Example**: " & @CRLF & _ " - Intervals = [[1, 4], [2, 6], [3, 8]], k = 2." & @CRLF & _ " - Output: 2." & @CRLF & _ "" & @CRLF & _ "```python" & @CRLF & _ "def max_k_interval_coverage(intervals, k):" & @CRLF & _ " points = []" & @CRLF & _ " for interval in intervals:" & @CRLF & _ " points.append((" & @CRLF & _ "" & @CRLF & _ "interval[0], 'start'))" & @CRLF & _ " points.append((interval[1], 'end'))" & @CRLF & _ " " & @CRLF & _ " points.sort()" & @CRLF & _ " coverage = 0" & @CRLF & _ " max_coverage = 0" & @CRLF & _ " " & @CRLF & _ " for point, kind in points:" & @CRLF & _ " if kind == 'start':" & @CRLF & _ " coverage += 1" & @CRLF & _ " if coverage == k:" & @CRLF & _ " max_coverage = max(max_coverage, coverage)" & @CRLF & _ " else:" & @CRLF & _ " coverage -= 1" & @CRLF & _ " " & @CRLF & _ " return max_coverage" & @CRLF & _ "" & @CRLF & _ "# Example" & @CRLF & _ "print(max_k_interval_coverage([[1, 4], [2, 6], [3, 8]], 2)) # 2" & @CRLF & _ "```" & @CRLF & _ "" & @CRLF & _ "---" & @CRLF & _ "" & @CRLF & _ "### 5. **Geometric Interval Problems**" & @CRLF & _ "" & @CRLF & _ "#### a) **Rectangle Intersection via Intervals**" & @CRLF & _ "" & @CRLF & _ "- **Problem Definition**: Given two rectangles, where each rectangle is defined by two intervals (x-axis and y-axis), find the intersection of these rectangles." & @CRLF & _ "" & @CRLF & _ "- **Approach**: " & @CRLF & _ " - Find the intersection of intervals on both x and y axes. The intersection of two rectangles is the combination of the intersecting intervals on both axes." & @CRLF & _ "" & @CRLF & _ "- **Example**: " & @CRLF & _ " - Rectangle A = ([1, 5], [2, 6]), Rectangle B = ([3, 7], [4, 9])." & @CRLF & _ " - Output: [[3, 5], [4, 6]]." & @CRLF & _ "" & @CRLF & _ "```python" & @CRLF & _ "def rectangle_intersection(A, B):" & @CRLF & _ " x_intersect = intersection_intervals([A[0][0], A[0][1]], [B[0][0], B[0][1]])" & @CRLF & _ " y_intersect = intersection_intervals([A[1][0], A[1][1]], [B[1][0], B[1][1]])" & @CRLF & _ " " & @CRLF & _ " if x_intersect and y_intersect:" & @CRLF & _ " return [x_intersect, y_intersect]" & @CRLF & _ " return None" & @CRLF & _ "" & @CRLF & _ "# Example" & @CRLF & _ "A = ([1, 5], [2, 6])" & @CRLF & _ "B = ([3, 7], [4, 9])" & @CRLF & _ "print(rectangle_intersection(A, B)) # [[3, 5], [4, 6]]" & @CRLF & _ "```" & @CRLF & _ "" & @CRLF & _ "---" & @CRLF & _ "" & @CRLF & _ "This version includes **problem definitions**, **approaches**, and **examples** for all the interval problems, along with code implementations. Each section is structured to make it easier to understand and apply to similar problems." Local $sSubst = "\#\1\2" Local $sResult = StringRegExpReplace($sString, $sRegex, $sSubst) MsgBox($MB_SYSTEMMODAL, "Result", $sResult)

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm