XhCode Online Converter Tools

Regex Tester and generator

Common Regular Expressions

Check digit expressions

  • Digit:
    ^[0-9]*$
  • N digits:
    ^\d{n}$
  • At least N digits:
    ^\d{n,}$
  • m-n digits:
    ^\d{m,n}$
  • Zero and non-zero start digits:
    ^(0|[1-9][0-9]*)$
  • Nonzero number with up to two decimal places:
    ^([1-9][0-9]*)+(.[0-9]{1,2})?$
  • A positive or negative number with one or two decimal places:
    ^(\-)?\d+(\.\d{1,2})?$
  • Positive, negative, and decimals:
    ^(\-|\+)?\d+(\.\d+)?$
  • A positive real number with two decimal places:
    ^[0-9]+(.[0-9]{2})?$
  • A positive real number with 1 to 3 decimal places:
    ^[0-9]+(.[0-9]{1,3})?$
  • A non-zero positive integer:
    ^[1-9]\d*$  or  ^([1-9][0-9]*){1,3}$  or  ^\+?[1-9][0-9]*$
  • A non-zero negative integer:
    ^\-[1-9][]0-9"*$  or  ^-[1-9]\d*$
  • Non-negative integers:
    ^\d+$  or  ^[1-9]\d*|0$
  • Non-positive integer:
    ^-[1-9]\d*|0$  or  ^((-\d+)|(0+))$
  • Non-negative floating-point numbers:
    ^\d+(\.\d+)?$  or  ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
  • Non-positive floating-point number:
    ^((-\d+(\.\d+)?)|(0+(\.0+)?))$  or  ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
  • Floating point number:
    ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$  or  ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
  • Negative Float:
    ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$  or  ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
  • Floating point number:
    ^(-?\d+)(\.\d+)?$  or  ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$

Check character expressions

  • Alpha-Numeric Characters:
    ^[A-Za-z0-9]+$  or  ^[A-Za-z0-9]{4,40}$
  • All characters with a length of 3-20:
    ^.{3,20}$
  • A string of 26 letters:
    ^[A-Za-z]+$
  • A string of 26 uppercase English letters:
    ^[A-Z]+$
  • A string of 26 lowercase alphabetic characters:
    ^[a-z]+$
  • A string of numbers and 26 letters:
    ^[A-Za-z0-9]+$
  • A string of numbers, 26 letters, or an underscore:
    ^\w+$  or  ^\w{3,20}$
  • input with ^%&',;=?$\":
    [^%&',;=?$\x22]+
  • It is forbidden to input characters with ~:
    [^~\x22]+

Special Needs Expressions

  • Email:
    ^[a-zA-Z0-9_.+-][email protected][a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$
  • URL or Domain name:
    ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
  • Date (MM/DD/YYYY)/(MM-DD-YYYY)/(MM.DD.YYYY)/(MM DD YYYY):
    ^(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}$
  • 12 months of the year(01~09和1~12):
    ^(0?[1-9]|1[0-2])$
  • 31 days a month(01~09和1~31):
    ^(0[1-9]|[12][0-9]|3[01])$
  • Password:
    ^.*(?=.{6,})(?=.*d)(?=.*[A-Z])(?=.*[a-z])(?=.*[[email protected]#$%^&*? ]).*$
  • US Phone Numbers
    \b\d{3}[-.]?\d{3}[-.]?\d{4}\b
  • US Zip code
    ^[0-9]{5}(?:-[0-9]{4})?$
  • Slug
    ^[a-z0-9-]+$
  • All the special characters need to be escaped
    /[\-\[\]\/\\\{\}\(\)\*\+\?\.\^\$\|]/
  • xml file:
    ^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$
  • Regular expressions for Chinese characters:
    [\u4e00-\u9fa5]
  • Double-byte characters:
    [^\x00-\xff] 
  • Blank line:
    \n\s*\r    (be used to delete blank lines)
  • HTML tags:
    <(\S*?)[^>]*>.*?|<.*? />
  • The leading and trailing whitespace character:
    ^\s*|\s*$ or (^\s*)|(\s*$)
  • IP address:
    ((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))
Regex Tester and Regex code generator

A Regex Tester and Regex Code Generator is a tool that helps users test regular expressions (regex) and generate the corresponding code to use those regex patterns in different programming languages.

Key Features of Regex Tester and Code Generator:
Regex Tester: Allows you to input a regex pattern and a test string to see how the regex matches (or doesn't match) the string. This helps in debugging or refining regular expressions.
Code Generator: Generates the code that you can use to apply the regex pattern in various programming languages such as Python, JavaScript, Java, PHP, etc.
Steps to Create a Regex Tester and Code Generator:
1. Regex Tester:
The tester will:

Allow the user to input a regex pattern.
Allow the user to input a test string.
Display the match results (e.g., the matched substrings, the number of matches, or whether the pattern was found in the string).
Here's a simple Python implementation using the re library to test a regex pattern:

python

import re

def test_regex(pattern, test_string):
# Compile the regex pattern
regex = re.compile(pattern)

# Find matches in the test string
matches = regex.findall(test_string)

# Return the matches or a message if no match is found
if matches:
return matches
else:
return "No matches found."

# Example usage
pattern = r"\d{3}-\d{2}-\d{4}" # Pattern to match Social Security Number (SSN)
test_string = "My SSN is 123-45-6789"
matches = test_regex(pattern, test_string)
print("Matches:", matches)
Example Output:
sql

Matches: ['123-45-6789']
2. Regex Code Generator:
The code generator will take a regex pattern and automatically generate code for different programming languages. Below is an example of how the code generation might look for Python, JavaScript, and PHP.

Regex Pattern: r"\d{3}-\d{2}-\d{4}" (to match a Social Security Number)

Python:
python

import re

pattern = r"\d{3}-\d{2}-\d{4}"
test_string = "My SSN is 123-45-6789"
matches = re.findall(pattern, test_string)

print(matches)
JavaScript:
javascript

const pattern = /\d{3}-\d{2}-\d{4}/;
const testString = "My SSN is 123-45-6789";
const matches = testString.match(pattern);

console.log(matches);
PHP:
php

<?php
$pattern = '/\d{3}-\d{2}-\d{4}/';
$testString = "My SSN is 123-45-6789";
preg_match($pattern, $testString, $matches);

print_r($matches);
?>
Implementing a Simple Regex Tester and Code Generator in Python:
python

import re

def test_regex(pattern, test_string):
"""Test if the regex pattern matches the test string."""
regex = re.compile(pattern)
matches = regex.findall(test_string)

if matches:
return matches
else:
return "No matches found."

def generate_code(pattern, language="Python"):
"""Generate code based on the selected language."""
if language == "Python":
return f"""
import re

pattern = r"{pattern}"
test_string = "Your test string here"
matches = re.findall(pattern, test_string)

print(matches)
"""
elif language == "JavaScript":
return f"""
const pattern = /{pattern}/;
const testString = "Your test string here";
const matches = testString.match(pattern);

console.log(matches);
"""
elif language == "PHP":
return f"""
<?php
$pattern = '/{pattern}/';
$testString = "Your test string here";
preg_match($pattern, $testString, $matches);

print_r($matches);
?>
"""
else:
return "Language not supported."

# Example usage
pattern = r"\d{3}-\d{2}-\d{4}"
test_string = "My SSN is 123-45-6789"
print("Matches:", test_regex(pattern, test_string))

# Generate code for different languages
print("Python Code:\n", generate_code(pattern, "Python"))
print("JavaScript Code:\n", generate_code(pattern, "JavaScript"))
print("PHP Code:\n", generate_code(pattern, "PHP"))
Example Output:
php

Matches: ['123-45-6789']
Python Code:

import re

pattern = r"\d{3}-\d{2}-\d{4}"
test_string = "Your test string here"
matches = re.findall(pattern, test_string)

print(matches)

JavaScript Code:

const pattern = /\d{3}-\d{2}-\d{4}/;
const testString = "Your test string here";
const matches = testString.match(pattern);

console.log(matches);

PHP Code:

<?php
$pattern = '/\d{3}-\d{2}-\d{4}/';
$testString = "Your test string here";
preg_match($pattern, $testString, $matches);

print_r($matches);
?>
Use Cases for Regex Tester and Code Generator:
Learning & Education: Helps students and beginners understand how regular expressions work and how they are used in different programming languages.
Development: Useful for developers who frequently work with regex patterns and need to test them or convert them into code for different programming languages.
Debugging: If you're facing issues with regular expressions, a tester can help quickly verify and adjust your regex.
Cross-Language Conversion: Quickly convert a regex pattern from one language to another (e.g., Python to JavaScript).

TOP