How to use & create regex in setting up conditions
1. What is Regex? A Simple Explanation
Think of Regex as a "superpowered Find & Replace." It's a sequence of special characters that defines a search pattern.
Instead of just finding a specific word, Regex can find complex patterns in text.
A Regular Expression is a sequence of characters that specifies a search pattern. It's a formal language used in virtually every programming language and many text editors to find, validate, extract, and manipulate text.
Instead of matching literal text, a regex pattern can include special characters and syntax to match things like:
- A specific number of characters.
- A character from a certain range (e.g., a-z, 0-9).
- The beginning or end of a line.
- Repeated characters.
- And much more.
Practical E-commerce Examples:
- Validate phone numbers: Ensure a customer enters 10 digits and a valid area code.
- Block invalid email addresses: Prevent emails that are missing an
@
symbol or a domain like.com
. - Restrict words in order notes: Automatically detect and block sensitive words or spam links.
- Validate postal/ZIP code formats: Ensure the code matches the format for a specific country.
However, there's no perfect Regex. What we can do is to make regex become most relevant and A.I can help you with that!
2. How Blockify's AI Regex Generator Works
Instead of writing these complex character strings yourself, you simply describe your requirement in plain English. Our Artificial Intelligence (AI) will then "translate" that description into the correct Regex code.
Example:
You describe: "Create a regex for a US phone number, which can be in the format (123) 456-7890 or 123-456-7890."
The AI returns: ^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$
Step 1: Describe your prompt or use the prompt template, then click Create Regex
Step 2: Check regex value
Step 3: Copy regex and paste on Value
See how to use A.I to create Regex here
3. How to Write Effective Prompts for the Best Results
The quality of the Regex generated by the AI is directly related to the quality of your prompt. Here are the most important tips for success.
Tip 1: Be as Specific as Possible
The AI can't read your mind. Provide as much detail as you can about the pattern you want to match.
- Vague Prompt:
check for a phone number
- Better Prompt:
create a regex for a 10-digit US phone number, optionally with parentheses around the area code and dashes or spaces as separators
- Vague Prompt:
find a product code
- Better Prompt:
create a regex to find a product code that starts with 'SKU-', followed by exactly 6 digits. For example: SKU-123456
Tip 2: Provide Examples (Both Valid & Invalid Data)
This is one of the most effective ways to "teach" the AI what you want. Show it examples of what should match and what shouldn't.
- Excellent Prompt:
Create a regex to validate our order ID format.
Valid examples: PO-2024-001, PO-2025-987
Invalid examples: a-123, PO-123, PO-2024-1
Tip 3: Describe the "What," Not Just the "Why"
Focus on describing the structure of the text string you want to find, rather than your business goal.
- Vague Prompt:
create a regex to stop customers from spamming links in the notes
(This is the "why"). - Better Prompt:
create a regex to find any string that contains 'http://' or 'https://'
(This is the "what").
Tip 4: Start Simple, Then Refine
If you have a complex requirement, break it down. Start with a simple prompt to get a basic Regex, then ask the AI to refine it.
- Step 1:
create a regex that finds a string with at least 8 characters
- Step 2 (refinement):
now, modify the regex to require at least one uppercase letter and one number
Tip 5: Clearly State What to Exclude
Sometimes it's easier to describe what you don't want.
- Good Prompt:
Create a regex for a username that can only contain letters and numbers, and no special characters.
- Good Prompt:
Create a regex that matches any line that does not start with the word 'Shipping'
4. Notes
- Be Clear About 'AND' vs. 'OR'
This is the most common pitfall. Clearly distinguish between conditions that must all be true (AND) and conditions where only one needs to be true (OR).
- "AND" Logic (All conditions must be met): Use words like "and," "must also," "as well as," "must include both... and...".
- Good Prompt:
Create a regex for a password that must be at least 8 characters long, **and** must contain at least one uppercase letter **and** one number.
- "OR" Logic (Only one condition needs to be met): Use words like "or," "either... or...".**
Good Prompt: `Create a regex to find the words "promo", **or "discount", or "sale".
Ambiguous Prompt: Find orders from New York, California.
Then AI will understand it's AND
- Handle Whitespace
Users might type extra or inconsistent spaces. Plan for this in your prompt.
- Vague Prompt:
Find the gift card code 'GIFT 123'.
- Better Prompt:
Find a gift card code that starts with 'GIFT', followed by **one or more spaces**, and then 3 digits.
- Mention Special Characters Literally
Characters like .
*
+
?
()
[]
{}
have special meanings in Regex. If you want to match the actual character itself, be explicit.
- Vague Prompt:
Find products with a dot in the name.
- Better Prompt:
Create a regex to find a string that contains a **literal dot character (.)**.
(Specifying "literal character" helps the AI understand you want to find the period, not use it as a wildcard).
Examples
Here are some prompts you can copy and use right away:
- Prompt for AI Regex Generation for California Zip Codes
Generate a regular expression (regex) that matches valid 5-digit U.S. zip codes specifically for the state of California. California zip codes range from 90001 to 96162. The regex should:
- Only match numbers within this range (from 90001 to 96162).
- Not match zip codes outside of California or with fewer or more than 5 digits.
- Only allow numeric digits (no spaces, letters, or special characters).
- Prompt to create regex for Province code in India
Generate a regular expression (regex) that matches valid Indian province (state and union territory) codes. These codes are two uppercase letters as per the ISO 3166-2:IN standard (e.g., AP, AR, AS, BR, CG, GA, GJ, HR, HP, JH, KA, KL, MP, MH, MN, ML, MZ, NL, OD, PB, RJ, SK, TN, TG, TR, UP, UK, WB, AN, CH, DN, DD, DL, JK, LA, LD, PY). The regex should:
- Match only these valid two-letter uppercase codes.
- Not allow lowercase letters, numbers, or extra characters.
- Not match codes outside the official list.
The AI-powered Regex feature is a new and powerful tool. We are still in the process of building and improving it based on how you use it.
So please, experiment, explore, and don't hesitate to send us your feedback through Blockify's support channel. Your insights are incredibly valuable and will help us make the product even better.
Happy customizing
Updated on: 17/07/2025
Thank you!