IT Blog

  • Blog
  • Technology
    • Technology
    • Architecture
    • CMS
    • CRM
    • Web
    • DotNET
    • Python
    • Database
    • BI
    • Program Language
  • Users
    • Login
    • Register
    • Forgot Password?
  • ENEN
    • 中文中文
    • ENEN
Experience IT
In a World of Technology, People Make the Difference.
  1. Home
  2. Technology
  3. Common Regular Expressions

Common Regular Expressions

2022-02-23 122 Views 1 Like 0 Comments

Table of Contents

  • Line with certain chars
  • All Letters in all languages (Except 2E00 - 2FFF):
  • Chinese chars:
  • Punctuations (Chinese and English):
  • All HTML tags:
  • All HTML attributes:
  • URL
  • Email
  • Phone Number
    • North America
    • Credit Card# (from stackoverflow)
  • SIN#

Line with certain chars

//e.g. line contains "hostlike"
(?<=^)[^\n]*?hostlike[^\n]*(?=$)

All Letters in all languages (Except 2E00 - 2FFF):

\p{L}

Chinese chars:

\p{Han}

Punctuations (Chinese and English):

\p{P}

e.g. (punctuation + some special chars) (matched in PHP, C#):
!"#%&'()*,-./:;?@[\]_{}¡§«·»¿;։֊؊٫٬‘’‚‛“”„‟′″‴‵‶‷%‰‱
、。〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰゠・
︐︑︒︓︔︕︖︗︘︙﹅﹆﹇﹈﹉﹊﹋﹌﹍﹎﹏﹐﹑﹒﹙﹚﹛﹜﹝﹞﹟﹠﹡﹣()*,-./:;?@[\]_{}⦅⦆。「」、・
—―‖‗†‡•‣․‥…‧‸‹›※‼‽‾‿⁀⁁⁂⁃⁄⁅⁆⁇⁈⁉⁊¶⁋⁌⁍⁎⁏⁐⁑⁓⁔⁕⁖⁗⁘⁙⁚⁛⁜⁝⁞〈〉⌈⌉⌊⌋❨❩❪❫❬❭❮❯❰❱❲❳❴❵⟅⟆⟦⟧⟨⟩⟪⟫⟬⟭⟮⟯⦃⦄⦅⦆⦇⦈⦉⦊⦋⦌⦍⦎⦏⦐⦑⦒⦓⦔⦕⦖⦗⦘⧘⧙⧚⧛⧼⧽⳹⳺⳻⳼⳾⳿⸀⸁⸂⸃⸄⸅⸆⸇⸈⸉⸊⸋⸌⸍⸎⸏⸐⸑⸒⸓⸔⸕⸖⸗⸘⸙⸚⸛⸜⸝⸞⸟⸠⸡⸢⸣⸤⸥⸦⸧⸨⸩⸪⸫⸬⸭⸮ⸯ⸰⸱⸲⸳⸴⸵⸶⸷⸸⸹⸼⸽⸾⸿⹀⹁⹂

All HTML tags:

</?\w.*?>

All HTML attributes:

\s+\w+=[\w\W\s]+?(?=>)

URL

(https?:)?\/\/(www\.)?[\d\w.-]{2,256}\.\w{2,6}\/[\p{L}@:%_.~#?&=\d\w\-\+\/]*

Email

\b[\d\w._%+-]+@[\d\w.-]+\.[\w]{2,6}\b

Phone Number

North America

(\+?\d{1,2})?[-. ]?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}

Credit Card# (from stackoverflow)

  • Amex Card: ^3[47]\d{13}$
  • BCGlobal: ^(6541|6556)\d{12}$
  • Carte Blanche Card: ^389\d{11}$
  • Diners Club Card: ^3(?:0[0-5]|[68]\d)\d{11}$
  • Discover Card: ^65[4-9]\d{13}|64[4-9]\d{13}|6011\d{12}|(622(?:12[6-9]|1[3-9]\d|[2-8]\d\d|9[01]\d|92[0-5])\d{10})$
  • Insta Payment Card: ^63[7-9]\d{13}$
  • JCB Card: ^(?:2131|1800|35\d{3})\d{11}$
  • KoreanLocalCard: ^9\d{15}$
  • Laser Card: ^(6304|6706|6709|6771)\d{12,15}$
  • Maestro Card: ^(5018|5020|5038|6304|6759|6761|6763)\d{8,15}$
  • Mastercard: ^(5[1-5]\d{14}|2(22[1-9]\d{12}|2[3-9]\d{13}|[3-6]\d{14}|7[0-1]\d{13}|720\d{12}))$
  • Solo Card: ^(6334|6767)\d{12}|(6334|6767)\d{14}|(6334|6767)\d{15}$
  • Switch Card: ^(4903|4905|4911|4936|6333|6759)\d{12}|(4903|4905|4911|4936|6333|6759)\d{14}|(4903|4905|4911|4936|6333|6759)\d{15}|564182\d{10}|564182\d{12}|564182\d{13}|633110\d{10}|633110\d{12}|633110\d{13}$
  • Union Pay Card: ^(62\d{14,17})$
  • Visa Card: ^4\d{12}(?:\d{3})?$
  • Visa Master Card: ^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14})$

Remove all non-digits from the string, then use this for Visa, MasterCard, American Express, Diners Club, Discover, and JCB cards:

^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$

As of 2016-10-14 the MasterCard regex here will be insufficient as they will be adding a new range of 222100-272099. I just added the following for our validation (Slagmoth):

^(?:5[1-5][0-9]\d{1}|222[1-9]|2[3-6][0-9]\d{1}|27[01][0-9]|2720)([\ \-]?)\d{4}\1\d{4}\1\d{4}$

Note: Switch hasn't existed since 2002, Laser was withdrawn in 2014, Visa are due to issue 19 digit cards and MasterCard are now issuing in the 2xxxxx ranges.

Ref:

https://stackoverflow.com/questions/9315647/regex-credit-card-number-tests
https://www.regular-expressions.info/creditcard.html
https://web.archive.org/web/20180904130300/https://creditcardjs.com/credit-card-type-detection

SIN#

\d{3}( |-)?\d{3}( |-)?\d{3}

 609 total views,  4 views today

error
fb-share-icon
Tweet
fb-share-icon
IT Team
Author: IT Team

Tags: None
Last updated:2022-05-11

IT Team

This person is lazy and left nothing

Like
< Previous

Comments

Cancel reply
Chinese (Simplified) Chinese (Simplified) Chinese (Traditional) Chinese (Traditional) English English French French German German Japanese Japanese Korean Korean Russian Russian
Newest Hotspots Random
Newest Hotspots Random
All Unicode Chars How to keep and display contact form 7 data Common Regular Expressions How to block bad bots in Appache server Add ALT attribute to Social Media Icon Change Event Calendar Title
All Unicode Chars
Null values concerns in SQL Server Controlling posts order in plugin WP Tab Widget Wordpress + polylang - Globalization Adding reCaptcha for user forms in WordPress Generating XML SiteMap Generate city list for UPDATE statement in MySQL
Categories
  • Architecture
  • BI
  • C#
  • CSS
  • Database
  • DotNET
  • Hosting
  • HTML
  • JavaScript
  • PHP
  • Program Language
  • Python
  • Security
  • SEO
  • Technology
  • Web
  • Wordpress

COPYRIGHT © 2021 Hostlike IT Blog. All rights reserved.

This site is supported by Hostlike.com