Logo

dev-resources.site

for different kinds of informations.

How to work with regular expressions

Published at
12/7/2024
Categories
regex
validation
data
text
Author
xinitd
Categories
4 categories in total
regex
open
validation
open
data
open
text
open
Author
6 person written this
xinitd
open
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 mode Exit 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 mode Exit 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 mode Exit fullscreen mode

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

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 mode Exit fullscreen mode

Result:

Image description

Thanks for regex101.com site, which allowed test my regular expressions.


Are my posts is helpful? You may support me on Patreon.

regex Article's
30 articles in total
Favicon
Here are 7 Regex tools that can save your life from hell πŸ”₯
Favicon
What are the benefits of using bounded quantifiers in regex
Favicon
Understanding Regex in Python: A Practical Example
Favicon
Coding challenge: Design and Implement an Advanced Text Search System
Favicon
Automating Email Validation with Python: A Step-by-Step Tutorial
Favicon
Streaming regex scanner β€” regexpscanner
Favicon
Unraveling the Magic of Regular Expressions: The Ultimate Guide to Mastering Sed, Gawk, and POSIX PatternsπŸš€
Favicon
Masking confidential data in prompts using Regex and spaCy
Favicon
Regular Expressions for Highlighting Comments in PyCharm
Favicon
Regex lookahead
Favicon
Easy to follow Regular Expression Cheatsheet
Favicon
πŸ“ Cross-Post Project Update: Regex, Bug Fixes, and More Regex!
Favicon
How to work with regular expressions
Favicon
Advent of Code 2024 - Day 3: Mull it Over
Favicon
Vim Regex Tricks - Capitalize Every First Letter
Favicon
Finally figured out a whole bunch of Nginx regex. It's more confusing than normal regex somehow
Favicon
From Regex Rampage to Lazy Bliss: My rjq Performance Adventure
Favicon
Regular Expressions
Favicon
Building a Regex Engine in Go: Introducing MatchGo
Favicon
Build up your confidence with Regex: 5 Techniques to make it STICK
Favicon
Mastering Regular Expressions: A Semantic Approach to Regex
Favicon
Regex for a Java Software Engineer
Favicon
Intro to Regular Expressions
Favicon
Intro to Regular Expressions
Favicon
The importance of the environment in Regex pattern matching
Favicon
js / ts - expressΓ£o regular
Favicon
A Guide to Splitting Strings in JavaScript by Regex
Favicon
Taming the Regex Beast: A Beginner's Guide to Regular Expressions
Favicon
The JS string replace() method
Favicon
Learn Enough Regex Without Losing Your Mind

Featured ones: