Hi my all are dev & tech beautiful friends
you're thinkingš¤ why I've written the title of regex is like an egg no worries I'll be explaining that
a little bit of introduction of egg š„
Both the white and yolk of an egg are rich in nutrients, including proteins, vitamins, and minerals. The yolk also contains cholesterol, fat-soluble vitamins (such as vitamins D and E), and essential fatty acids. Eggs are also an important and versatile ingredient for cooking wait! ā wait!
no worries, it's neither a food article nor I'm a food expert
as we know that the egg is very important in our health and the regex is important part of programming language.
my meet with regex
as a beginner, we have a lot of confusion about the programming language different concepts include with regex. personally, I had a little bit scared about the regex. that's why I tried to learn as much as possible. and I figured out the regex is how cool is that.
What Is a Regular Expression?
š£ļøw3schools says
A regular expression is a sequence of characters that forms a search pattern.
When you search for data in a text, you can use this search pattern to describe what you are searching for.
A regular expression can be a single character or a more complicated pattern.
Regular expressions can be used to perform all types of text search and text replace operations.
It is used in almost all languages like C#, Java, Javascript, To, Python, so on.
if you want to learn more check out the link below š
Regular expressions javascript.info
Regular expressions MDN
Regular expressions w3schools
Regular Expression with Java, javaScript, python
in this article I'm not going to be teaching you the Regular Expression.
we would try to explore the regex with problem-solving also understand how does regex work.
I hope it would be better for us to understand the regex.
below is the Problem Statement
in this problem, we can solve several ways but we would solve it with regex.
let's do solve with javascript
In the above solution, now we are going to explore what's happening here. at first, we've taken the variable names of zero
and one
after that InstallShield with regex operation.
s
is an input parameter into our function and match()
is regex method, it takes a parameter likes a regex pattern.
// forward-slash:
in the javascript, we can write regex Pratten Through forward-slash also constructor function, it's calling literal /ab+c/
another it's calling constructor new RegExp('ab+c')
.
0+
0 is our expected value and +
it's calling Quantifiers, The plus sign indicates one or more occurrences of the preceding 0
element.
g flag modifier
it means g
is a modifier global flag
Note:in the javascript If the regex does not include the g modifier (to perform a global search), the match()
method will return only the first match in the string.
The match()
method searches a string for a match against a regex, and returns the matches, as an Array object, returns null if no match is found.
we got likes this [000]
from zero
and one
[1111]
as we saw that the match()
method returns null if no match is found. that's why we used after ||
or operator it helps to convert the null to the empty array []
after parentheses to grouping the expression we used join()
method it helps to convert to the string likes this '0000'
and '1111'
now we explore return
it just returns boolean value because we subtracted with zero and one length inside of math.abs function and meth.abs function return absolute value after that we compared less than or equal 1 or not thisās our solution with regex.
below is the Problem Statement
as we know, we can solve several ways but we would solve with regex.
let's do solve with python
In the above solution. now we are going to explore what's happening here.
in this problem if we want to make any operation with regex we have to import the re
module in python other ways we can't do any operation with regex.
whatever, at first we've taken the variables name of decoded
after InstallShield with regex method re.findall()
.
findall(pattern, string, flags=0)
method has up to three arguments.
The findall()
is probably the single most powerful function in the re module. findall()
finds *all* the matches and returns them as a list of strings, with each string representing one match.
note: If no matches are found, an empty list is returned.
then we have used the pattern[a-zA-Z1-9]
a-z
it means given the ipute a to z any alphabetic smil latter and as same as that A-Z
any alphabetic capital letter also 1-9
it means 1 to 9 any number to the match. if we can use flag I
we don't need to mention capital and smail latter.
however, we got this ['John', 'Doe', '123']
from decoded as a list now we can easily use with dictionary and we did that.
below is the Problem Statement
as we know that, we can solve several ways but we would solve it with regex
let's do solve with java
above the solution now we're going to explain that what's happening here.
as we see that ParsonlIdNumber class inside of the validate function which returns boolean, also we need just return boolean.
pin
is a parameter associated with matches method matches()
is building method in java.
however. at first, we have used ^
it means Matches the beginning of a line.
()
it means Capturing group: Matches and remembers the match.
\d
it means Matches any digit. Equivalent to [0-9]. also, The extra \
in \\d
is used to escape the backslash from the string.
{}
it means Matches n number repetitions of the previous character or expression.
|
it means Matches either "x" or "y". we can relate like a boolean OR. Matches the expression before or after.
$
it means Matches the end of the line.
how did this pattern ^(\\d{4}|\\d{6})$
work.
start with the number 4 or 6 time with ending. that's our result.
end of this article: Keep Learning
Thatās the end of this article, but keep learning and practicing.
I've tried to a couple of examples with solved the problem, because if we see some use cases it really helpful for us to understanding any particular concept that's why I solved the problem by exploring how does work regex.
I don't know could I explain properly or not but I tried as much as possible to explained. if I've mistaken anything please share it with me I'll correct it.
I really passionate about sharing my knowledge Through write an article and in the future, I'll try to write another concept article until stay safe.
happy codingāļø
Top comments (5)
Well blow me that problem statement got blown three times in one article, it must be both satisfied and tired! šš¤£š¤£
Did you mean below is the problem statement?
Thank you for pointing me out wrong.
well yes I tried to write * below is the problem statement* but it was my mistake.
and please could you tell me how can I overlook the word three times as the same thing.
however, it's my first article that's why I don't know how can Write a Neatly article
Thanks again
Itās ok itās super easy to do it, I was just being silly ā¤ļø
Nice one. Thanks for sharing.
Thank you so much for commentš