Let's Talk About REGEX !!
Tags: career, java, beginners, tutorial
Published: 2020-08-05 12:00:27

## I think most of us would agree that :

**"Regex"** is one of those concepts in computer science that no matter how many times you practice , you may have to go back to your notes or google about it. :worried:
 ---

## What are regex/regular expressions :question: :confused:

A regular expression defines a search pattern for strings. The abbreviation for regular expression is regex. The search pattern can be anything from a simple character, a fixed string or a complex expression containing special characters describing the pattern. The pattern defined by the regex may match one or several times or not at all for a given string.
 
 ---

##  :boom: Common matching symbols:

| Regular Expression      | Description |
| ------------------------| ----------- |
| .                       |     matches any character   |
| ^regex               | Finds regex that must match at the beginning of the line.     |
|regex$                   |Finds regex that must match at the end of the line.|
|[abc]                    |Set definition, can match the letter a or b or c.|
|[abc][vz]                |Set definition, can match a or b or c followed by either v or z.|
|[^abc]                   | When a caret appears as the first character inside square brackets, it negates the pattern. This pattern matches any character except a or b or c.|
|[a-d1-7]                 | Ranges: matches a letter between a and d and figures from 1 to 7, but not d1.|
|X|Z                      |Finds X or Z.|
|XZ                       | Finds X directly followed by Z.|
|$                        |Checks if a line end follows.|

---
##  :boom: Meta characters:


| Regular Expression      | Description |
| ------------------------| ----------- |
| \d                      |Any digit, short for [0-9]|
| \D                      |A non-digit, short for [^0-9]|
|\s                       |A whitespace character, short for [ \t\n\x0b\r\f]|
|\S                       |A non-whitespace character|
|\w                       | A word character, short for [a-zA-Z_0-9]|
|\W                       | A non-word character [^\w]|
|\S+                      |Several non-whitespace characters|
|\b                       |Matches a word boundary where a word character is [a-zA-Z0-9_]|

 :dart: **[NOTE]:** These meta characters have the same first letter as their representation, e.g., digit, space, word, and boundary. Uppercase symbols define the opposite.
 
 ---

##  :boom: Quantifier:

A quantifier defines how often an element can occur. The symbols ?, *, + and {} are qualifiers.


| Regular Expression      | Description |
| ------------------------| ----------- |
|*                        |	Occurs zero or more times, is short for {0,}|
|+                        |	Occurs one or more times, is short for {1,}|
|?                        |	Occurs no or one times, ? is short for {0,1}.|
|{X}                      |Occurs X number of times, {} describes the order of the preceding liberal|
|{X,Y}                    |	Occurs between X and Y times|
|*?                       |? after a quantifier makes it a reluctant quantifier. It tries to find the smallest match. This makes the regular expression stop at the first match.| 
 ---

 :dart: **[NOTE]** - The backslash  \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w, then you must be using \\w in your regex. If you want to use backslash as a literal, you have to type \\\\ as \ is also an escape character in regular expressions.
 ---

##  :boom: Redefined methods on String for processing regular expressions.

| Regular Expression      | Description |
| ------------------------| ----------- |
|s.matches("regex")       | Evaluates if "regex" matches s. Returns only true if the WHOLE string can be matched.|
|s.split("regex")         |Creates an array with substrings of s divided at occurrence of "regex". "regex" is not included in the result.|
|s.replaceFirst("regex"), "replacement" | Replaces first occurance of "regex" with "replacement.|
|s.replaceAll("regex"), "replacement"  | 	Replaces all occurances of "regex" with "replacement.|

---


[Practice your Regex Skills here](https://www.hackerrank.com/domains/regex)
[Cheatsheet](https://www.jrebel.com/blog/java-regular-expressions-cheat-sheet)

Some resources gathered from the discussions panel:
[regex101](https://regex101.com/)
[regexr](https://regexr.com/)
[ihateregex.io](https://ihateregex.io/)

Thank you [Madza](https://dev.to/madza) , [Jakeer](https://dev.to/jakeerc) for the links.

---

Some of my other posts:


| Concept   |      link    |  
|----------|:-------------:|
| Java Access Modifiers |  [goto Article](https://dev.to/the_unconventional_coder/java-concept-of-the-day-1jim) | 
| Java Generics |  [goto Article](https://dev.to/the_unconventional_coder/java-concept-of-the-day-oec)     |  
|Java Regex     | [goto Article](https://dev.to/the_unconventional_coder/lets-talk-about-regex-3npd)
| Java Streams Api | [goto Article](https://dev.to/the_unconventional_coder/lets-talk-about-streams-2bdf) | 

 ---

Please leave a :heart: if you liked this post!
A 🦄 would be great!

And feel free to  let me know  in the discussions if you think i missed something.

HAVE A GOOD DAY!