Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Shakhzhakhan Maxudbek
Shakhzhakhan Maxudbek

Posted on • Edited on • Originally published atargs.tech

How to work with regular expressions

What are regular expressions? These are patterns that help us work with text. Regular expressions (regex, regexp) used to match sequences of characters in strings: search, edit, delete any area in large texts, data generation or validation, and so on. Regexes can be used in programming languages, command line interfaces, etc...

For example I want validate email addresses from my domain. The expression will look like this:

^.*\@example\.com$
Enter fullscreen modeExit fullscreen mode

^ symbol means the beginning of an expression.
Next symbols.* need for matching any address in @example.com domain.
Backslash before "at" symbol need for validate@ as is. The same situation with\. symbols.
$ - end of expression.

Result:

Image description

Next case need for validation phone numbers. I need accept numbers only from my region. In my case numbers started from+77. I should use this expression:

^\+77\d{1,9}$
Enter fullscreen modeExit fullscreen mode

^ symbol means the beginning of an expression.
\+ - using "plus" symbol as "plus" symbol.
\d - presents d as digits.
In curly brackets{1,9} digits length.
$ - end of expression.

Result:

Image description

Additionally maybe validation when phone number starts from "+77" or "87":

^(\+77|87).?\d{1,9}$
Enter fullscreen modeExit fullscreen mode

(\+77|87).? - number starts from+77 or87.

Result:

Image description

Now let's look at IP address validation. For verification IP addresses you may use this expression:

((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}
Enter fullscreen modeExit fullscreen mode

Result:

Image description

Thanks forregex101.com site, which allowed test my regular expressions.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

System administrator, software engineer, technical blogger and open source contributor. Passionate about automation and cloud technologies.
  • Location
    Kazakhstan
  • Education
    Cisco Networking Academy
  • Work
    ARGS LAB
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp