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 371 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):
  • Chinese Phonetic Notation(注音符号):
  • 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}

or:

[\x{4e00}-\x{9fa5}]

C#:

var CHINESE1 = @"\p{IsCJKUnifiedIdeographsExtensionA}";
var CHINESE2 = @"\p{IsCJKUnifiedIdeographs}";
@$"[{CHINESE1}{CHINESE2}]"
Regex.IsMatch(name, @$"[{CHINESE1}{CHINESE2}]")  //true if name contains Chinese word.
3400-4DBF	IsCJKUnifiedIdeographsExtensionA
4DC0-4DFF	IsYijingHexagramSymbols
4E00-9FFF	IsCJKUnifiedIdeographs
3100-312F	IsBopomofo

Punctuations (Chinese and English):

\p{P}

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

Chinese Phonetic Notation(注音符号):

\p{IsBopomofo}

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}

Loading

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

Tags: None
Last updated:2023-05-31

IT Team

This person is lazy and left nothing

Like
< Previous

Comments

Cancel reply
Newest Hotspots Random
Newest Hotspots Random
Rich editor not working Making web page scroll down automatically Getting data from Dapper result All Unicode Chars How to keep and display contact form 7 data Common Regular Expressions
Special characters and symbols for HTML Controlling posts order in plugin WP Tab Widget Passing parameters to event handler, setTimeout Add ALT attribute to Social Media Icon Jetpack conflict with Crelly Slider The events calendar - event title location
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