Use Case | Conditions | | Explanation |
Check if the text is one of the exact phrases | In the following three sentences if you want to check if the text is one of exact given phrases: I want to buy a TV I want to return my order I want to check my refund status
| Then the following RegEx would work: ^I want to buy a TV$|^I want to return my order$|^I want to check my refund status$ | ^ in the RegEx denotes the sentence should start with “I want…” $ in the RegEx denotes the sentence should end with “....TV” | in the RegEx denotes OR, it means either sentence 1 or sentence 2 or sentence 3 This matches the exact phrases. If it has spaces before or after the sentence, it won’t match. |
Check if the text contains one of few phrases (need not be an exact match) | If you want to match: I want to buy a TV I want to return my order I want to check my refund status
For example, in the text, you want to match “Hey, I want to buy a TV soon”. | The following RegEx would work - (I want to buy a TV)|(I want to return my order)|(I want to check my refund status) | This matches the phrases in the text. Need not be exact match. ( ) contains text phrase | denotes OR So it’s either phrase-1 or phrase-2 or phrase-3 Limitations: This will also match "Rick and LIBI want to buy a TV" To ensure that the start and beginning of these phrases are not part of another word, you need to use: \b((I want to buy a TV)|(I want to return my order)|(I want to check my refund status))\b Here you have surrounded the phrases with the word boundary. (\b Word boundary can be a start, an end of a sentence, space , or a punctuation mark.) |
Check if a text contains a 7 digit number | If you want to check if a text contains a 7 digit number. | The following RegEx would work: [0-9]{7} | In this, digits between 0 to 9 are expected to repeat 7 times exactly. However, this will match 7 digits even if they are part of a word. Example apple1234567. To find the number separately you need to surround by a word boundary \b[0-9]{7}\b |
Check if a text contains 6 digit number in a specific format
| If the format is: AA-XXX-X-XX where A is an alphabet and X is a digit. | The following RegEx would work: \b[A-Z]{2}-[0-9]{3}-[0-9]{1}-[0-9]{2}\b | \b denotes the word boundary so the expression should be a separate word and not part of a larger word. [A-Z]{2} denotes that there should be two capital alphabets - denotes dash [0-9]{3} denotes 3 digits. |
Check for an email | If you want to check for an email. | The following RegEx would work: \b(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))\b | The entire regular expression ensures a particular format of alphanumeric expression to be matched. Limitations: Invalid email might also be captured by this, but this RegEx should work in most situations. |
Check for phone number | If you want to check for a phone number. A phone number can be of several formats: XXXXXXXXX XXX XXX XXXX XXX-XXX-XXXX (XXX) XXX XXXX (XXX)-XXX-XXX
| The following RegEx would work: \b\+?[0-9]{0,3}-?\s?(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}\b | The entire regular expression ensures a particular format of alphanumeric expression to be matched. |
Check for emoji-only in messages | If you want to check if a message contains only emoji. | The following RegEx would work: ^(([\x{1F000}-\x{1F98F}]|[\x{2B00}-\x{2BFF}]|[\x{2702}-\x{27B0}]|[\x{2100}-\x{23FF}]|[\x{1F68}-\x{1F6C}]|[\x{1F30}-\x{1F70}]|[\x{2600}-\x{26ff}]|[\x{D83C}-\x{DBFF}\x{DC00}-\x{DFFF}]|([\x{20E3}]))){1,}$ | The entire regular expression ensures a particular format of alphanumeric expression to be matched. |
Check if a message contains any emoji | If you want to check if a message contains any emoji | The following RegEx would work: ([\x{1F000}-\x{1F98F}]|[\x{2B00}-\x{2BFF}]|[\x{2702}-\x{27B0}]|[\x{2100}-\x{23FF}]|[\x{1F68}-\x{1F6C}]|[\x{1F30}-\x{1F70}]|[\x{2600}-\x{26ff}]|[\x{D83C}-\x{DBFF}\x{DC00}-\x{DFFF}]|([\x{20E3}])) | The entire regular expression ensures a particular format of alphanumeric expression to be matched. |
Check for specific emojis | If you want to check for specific emojis | | The entire regular expression ensures a particular format of alphanumeric expression to be matched. |