Regex is like an egg
Tags: programming, javascript, java, python
Published: 2021-07-03 12:03:42


#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](https://javascript.info/regular-expressions)
[Regular expressions MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
[Regular expressions w3schools](https://www.w3schools.com/js/js_regexp.asp)


###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
![blow the Problem Statement](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gwehokufinjhwx7f5a5.png)
in this problem, we can solve several ways but we would solve it with regex.

###let's do solve with javascript
![let's do solve with javascript](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xg54wsheombufry09uqr.png)
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
![blow the Problem Statement](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwq8cmsa1dhw6ow3x6ba.png)
as we know, we can solve several ways but we would solve with regex.
###let's do solve with python
![let's do solve with python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbzi0hmlrs8ij0vh0o5l.png)
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
![blow the Problem Statement](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69hpobc1h5749uzidtak.png)
as we know that, we can solve several ways but we would solve it with regex 

###let's do solve with java
![let's do solve with java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwxb3029wub22bcgrojr.png)
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✌️