Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
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
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"buffer_1", flags=re.MULTILINE) test_str = ("/*\n" " * To change this license header, choose License Headers in Project Properties.\n" " * To change this template file, choose Tools | Templates\n" " * and open the template in the editor.\n" " */\n" "package kythuatlaptrinhat;\n\n" "import java.util.Random;\n" "import java.util.Scanner;\n\n" "/**\n" " *\n" " * @author shadyside\n" " */\n" "public class Bai2Lab1 {\n\n" " public static void main(String[] args) {\n" " Scanner input = new Scanner(System.in);\n\n" " System.out.println(\"Nhap chieu dai mang:\");\n" " int inputLen = input.nextInt();\n" " int[] inputArray = inputArray(inputLen);\n\n" " System.out.println(\"Mảng nguồn: \");\n" " printArray(inputArray);\n" " System.out.println(\"Nhập điểm bắt đầu sao chép: \");\n" " int start = input.nextInt();\n" " System.out.println(\"Nhập điểm kết thúc: \");\n" " int end = input.nextInt();\n" " System.out.println(\"Nhập chiều dài mảng đích\");\n" " int outputLen = input.nextInt();\n" " int[] ouputArray = inputArray(outputLen);\n\n" " System.out.println(\"Nhập vị trí bắt đầu thêm dữ liệu\");\n" " int startOfCopy = input.nextInt();\n\n" " System.out.println(\"Mảng đích ban đầu: \");\n" " printArray(ouputArray);\n" " System.out.println(\"-------------------------\");\n" " System.out.println(\"Ket qua:\");\n" " System.out.println(\"Mảng nguồn: \");\n" " printArray(inputArray);\n" " int[] result = copyArray(inputArray, inputLen, start, end, ouputArray, outputLen, startOfCopy);\n" " if (result == null) {\n" " System.out.println(\"Có lỗi xảy ra\");\n" " } else {\n" " System.out.println(\"Mảng đích: \");\n" " printArray(result);\n" " }\n" " }\n\n" " public static int[] copyArray(int[] inputArray, int inputLen, int start, int end, int[] outputArray, int outpuLen, int startOfCopy) {\n" " if (start >= 0 && end <= inputLen && startOfCopy >= 0 && startOfCopy <= outpuLen && start <= end) {\n" " /*\n" " *Trường hợp số lượng cần copy trong mảng nguồn nhỏ hơn số phần tử còn trống trong mảng đích\n" " */\n" " if ((end - start + 1) < (outpuLen - startOfCopy - 1)) {\n" " /*\n" " * Thực hiện dịch các phần tử\n" " */\n" " for (int i = outpuLen; i > startOfCopy + (end - start + 1) + 1; i--) {\n" " outputArray[i - 1] = outputArray[i - 1 - (end - start + 1)];\n" " }\n" " /*\n" " * Copy các phần tử vào mảng đích\n" " */\n" " for (int i = start; i <= end; i++) {\n" " outputArray[startOfCopy + 1 + (i - start)] = inputArray[i];\n" " }\n" " System.out.println(\"So gia tri duoc sao chep: \" + (end - start + 1));\n" " }\n" " /*\n" " *Trường hợp số lượng cần copy trong mảng nguồn bằng số phần tử còn trống trong mảng đích\n" " */\n" " else if ((end - start + 1) == (outpuLen - startOfCopy - 1)) {\n" " for (int i = start; i <= end; i++) {\n" " outputArray[startOfCopy + (i - start)] = inputArray[i];\n" " }\n" " } \n" " /*\n" " *Trường hợp số lượng cần copy trong mảng nguồn lớn hơn số phần tử còn trống trong mảng đích\n" " */\n" " else {\n" " for (int i = start; i < start + (outpuLen - startOfCopy); i++) {\n" " outputArray[startOfCopy + (i - start)] = inputArray[i];\n" " }\n" " }\n" " }\n" " return outputArray;\n" " }\n\n" " public static int[] inputArray(int len) {\n" " int[] inputArray = new int[len];\n" " Random random = new Random();\n" " for (int i = 0; i < len; i++) {\n" " inputArray[i] = random.nextInt(100);\n" " }\n" " return inputArray;\n" " }\n\n" " public static void printArray(int[] a) {\n" " for (int i = 0; i < a.length; i++) {\n" " System.out.print(a[i] + \" \");\n" " }\n" " System.out.println(\"\");\n" " }\n\n" "}\n") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

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 Python, please visit: https://docs.python.org/3/library/re.html