Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Examples of Regular Expressions
3.
Frequently Asked Questions
3.1.
What is meant by regular expression?
3.2.
Write a regular expression for a set of vowels?
3.3.
What are Regular Language and Regular Grammar?
4.
Conclusion
Last Updated: Mar 27, 2024

Examples of Regular Expression

Author Anant Dhakad
0 upvote

Introduction

This blog will discuss some of the Examples of Regular Expressions. A regular expression (also known as a rational expression) is a character sequence that determines a text search pattern. String-searching algorithms typically use such patterns for string operations like "find" or "find and replace", as well as for input validation.

Regular expressions are a generalized method of matching patterns with character sequences. Every programming language, including C, Java, and Python, uses it.

Also See, Moore Machine, Arden's theorem

Examples of Regular Expressions

Let's now look at some examples of regular expressions.

→ Make a regular expression for a language that accepts all strings, which starts with 1 and ends with 0.

So this language will accept any string with 1 in the starting, 0 in the end, and a binary string in between. Therefore the regular expression will be:

R = 1 (0 + 1)* 0

→ Make a regular expression for a language that accepts all strings which start with 'a' but do not contain consecutive b's.

Here mentioned language L = [a, ab, aba, aab, ….. ]

Therefore the regular expression will be:

R = (ab + a)*

→ Make a regular expression for a language that accepts all strings which contain any number of a, b and c, and in the same order.

We know that any number of x's is expressed in regular expression as x*. Also given in the question that order is a, then b, and c at last. Therefore the regular expression will be:

R = a* b* c* 

→ Make a regular expression for a language that accepts all binary strings of even length which contains only zero.

As it is mentioned that the required strings must be of even length and should only contain zero.

So the language L = [ ∊, 00, 0000, …. ]. Therefore the regex will be:

R = (00)*

→ Make a regular expression for a language that accepts all binary strings which do not contain any substring of form 01.

As there should not be any 1 followed by a zero.

So the language L = [ ∊, 0, 1, 10, 100, 110, 1000, 1100, …]. Therefore the regex will be:

R = 1* 0*

→ Make a regular expression for a language that accepts all binary strings in which every zero (if any) is followed by 11.

So the language L = [ ∊, 1, 11, 011, …]. Therefore the regex will be:

R = (1 + 011)*  

→ Make a regular expression for a language that accepts all binary strings which have at least one '1' and one '0'.

So there should be at least one 1 and 0 in the string. Now there can be two cases.

i) whether 1 comes before 0

ii) whether 1 comes after 0

The regex that will include both the cases is:

R = (0+1)* 1 (0+1)* 0 (0+1)*   +   (0+1)* 0 (0+1)* 1 (0+1)*

Also check out - Substr C++

Also see, Turing Machine in TOC.

Frequently Asked Questions

What is meant by regular expression?

A regular expression can also be defined as a pattern sequence that represents a string. It is most commonly used in pattern matching with strings or string matching. 

Write a regular expression for a set of vowels?

The regular expression for a set of vowels is ( a ∪ e ∪ i ∪ o ∪ u ). 

What are Regular Language and Regular Grammar?

Grammar is considered regular if it has rules of the form A -> a or A -> aB or A -> É› where É›  is a special symbol known as NULL and a language is considered regular if it can be expressed using regular expressions. 

Conclusion

Cheers if you reached here!! 

In this article, we saw some examples of converting some pattern or set of alphabet/strings into regular expressions.

Check out some of our other blogs related to the Theory of Computation:


Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Cheers!

Live masterclass