REGEX - It's a Piece of Cake!

Subscribe to my newsletter and never miss my upcoming articles

Hello everyone!!๐Ÿ’

Are you confused with the title๐Ÿ˜…? Do you also stay away from regular expressions? Do you use google while solving regex problems? Not after reading this article!๐Ÿ˜‹

C'mon, let's learn it together.๐Ÿค—

CONTENTS

Why Regular Expressions?

Regex(Regular Expressions) is used to define various types of string patterns which can be used for manipulating, searching, or editing a string. It can be used in all types of text search and text replace operations. Simply, a regular expression is a sequence of characters that helps us to match or find strings.

As we are now aware of regex, let's see how to write it.๐Ÿคฉ

Cheatsheets

Matching Symbols

symbols.PNG

Note: [^regex] and ^regex are totally different.

Meta Characters

symbolsTwo.PNG

Quantifiers

symbolsthree.PNG

How to write regular expressions?

We should write a regular expression pattern in such a way that it satisfies the required output. Many of them feel regex is hard because of this. So, let's solve some examples to understand this more clearly.

disclaimer: I am using java methods in solutions below. If you are willing to use other languages just check with the regular expression, which is our main goal.๐Ÿ˜‰

  • Question-1: Given string is "I love Hashnode. It is a wonderful platform." Write a matching regular expression that split the string into tokens.
    String s="I love Hashnode. It is a wonderful platform." ;  
    s=s.replaceAll("[^\\w]", " ");
    
    As we take only words as tokens we have to remove . after each sentence. So, we used [^\\w] in replaceAll() to replace all characters except [a-zA-Z_0-9] with a white-space. Now, to split them into tokens we do๐Ÿ‘‡
    String[] str=s.split("[\\s]+");
    
    Here, we are scanning for a token until a white-space occurs. We identified white spaces using [\\s]. Since there can be a sequence of more than one white spaces we also used a +.

    slashs.JPG

Output: [I, love, Hashnode, It, is, a, wonderful, platform]
  • Question-2: Given a string "I l1ove Hashn7ode. It is 3a wond5erful pla4tform.". Our aim is to print only numbers from this string.
    String s="I l1ove Hashn7ode. It is 3a wond5erful pla4tform.";  
    s=s.replaceAll("[^0-9]", " ");
    
    Our aim is to get numbers right? So, I am replacing everything with a white-space expect 0-9 using [^0-9].
    s=s.trim();
    String[] str=s.split("[\\s]+");
    
    • Why we used trim()?

trim() is used to remove trailing and leading spaces. After replacing alphabets with a white-space, we get trailing spaces because of I in " I love Hashnode". Since trailing spaces cannot be identified with [\\s]+ we used trim().

Output:  [1, 7, 3, 5, 4]

That's all people๐Ÿ˜. Now, we can write any regex patterns in any language by using these cheat sheets.

Say bye-bye to google!๐Ÿ˜

Articles you may like

Denny Trebbin's photo

A good run down and easy to read :-)

I add some links I find useful:

Cheat Sheet

A vast collection of solutions

Generate RegExes from input data

Online Editors

Visualize RegExes

Sai Laasya Vabilisetty's photo

Wow!! I don't know some of these. Thanks for sharing.๐Ÿ˜Š Denny Trebbin Saul Blanco Tejero

Robert Beutner's photo

Fantastic resource! I am teaching a little about this right now and this is the perfect resource.

Sai Laasya Vabilisetty's photo

Glad this helped you๐Ÿ˜Š๐Ÿ˜Š

mcsee's photo

Nice Article !

Reminds me of a funny quote

"One day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?"

-Larry Wall

Sai Laasya Vabilisetty's photo

hehe thank you๐Ÿ˜‹

Your DevOps Guy's photo

For more advanced use cases, the Java classes I have mostly worked with are in https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html.

Nice and straightforward intro to regex, Sai Laasya Vabilisetty!

Sai Laasya Vabilisetty's photo

Thank you!๐Ÿ˜Š

Sรฉbastien Portebois's photo

๐Ÿ‘

Other useful tools: regex101.com After many years of using tools (I love regexes!), I settled down on this one (rather than others) for a few reasons:

  • easy support of whitespace (when you have to deal with more than just spaces)
  • support various languages. Not all regexes are exactly the same
  • code generation. Ease the process to escape things
  • built-in tests support!

regexplained.co.uk which builds a railroad diagram of the regex pattern you give it. Makes it easy to review/read/understand a new pattern.... as they grow they can become hard to read easily, this helps a lot!

Sai Laasya Vabilisetty's photo

Loved to see people coming and sharing helpful resources๐Ÿ˜๐Ÿคฉ. Thanks, for sharing.

Saul Blanco Tejero's photo

Why do you use a character set in this example?

String[] str=s.split("[\\s]+");

I mean, it is unnecessary in this case...

Anyway, I love to see people sharing the regex power :)

Sai Laasya Vabilisetty's photo

Yup Saul, I agree. I added so people who don't know and who are new to regex can also easily understand.๐Ÿ˜Š Thanks for feedback๐Ÿค—