Select Page

Introduction

A regular expression, also shortened as regex and regexp, is a type of searching pattern inside a text/code editor that supports it, which allows for a deep level of text manipulation. A regular expression can not only search for a given string, but can also work its way out to different types of strings in a given text at the same time. If you don’t know what I mean, here’s a quick demo. 

Let’s say you want to search for a word like organize inside a file containing text. As you should already know, British English uses z in such cases, so the word organize. Whereas in American English, the letter s is used, so the word organise. Now in a given piece of text, if you’re not sure which of the two is used, and you try to search for the one which has not been used in the text, you’re sure to mess things up, right? Here’s where a regular expression can come in handy.

When searching with a regular expression for the above given scenario, if you type out:

organi[sz]e

What this will do is search for both the instances, and if both of them exist, they’ll be highlighted altogether, without you needing to worry about searching it once again. How cool is that?

However, this example does seem a bit trivial. For now, let us focus on one of simplest forms of regex expression search (which actually is useful) – that is, to search for strings that match a given regex. We’ll see how things are done in a couple of different languages, so stay tuned till you find the one that you really need!


String contains regex example

The type of regex searching we’re going to cover as a whole in the article is very straightforward. Our task is to search for a string of text that contains the given regular expression. Regex can come in any form, but we’re going to cover only the simplest of them that’re possible. 

To give you the most basic idea of what a “string contains regex” scenario looks like, take into consideration the following example.

Let’s say that I want to search for the word Hello inside a big chunk of text. What should the regular expression for this search look like? You should’ve guessed it right – the word Hello itself. Yes, it’s that simple, at least on paper. However, if you were to achieve this inside a programming language like Python, it’s probably not going to be easy if you know the code required.


Regex search substring

Searching for a substring isn’t of much difference than the example we mentioned earlier. For example, let us consider a string called hotdog. There are two sub words (or sub strings) that can be formed from it, hot and dog. If you simply search for an expression like:

hot

the search result will highlight or print out only that part of the whole word. This example will seem more relevant when we look at examples that involve using regular expressions inside a programming language. 


Regex contains character

You can also search for single characters by defining regular expressions. There are a few ways to go about this.

The first one is that you simply want to find each occurrence of a given letter, such as ‘a’. Assume that you have a sentence string that contains the following words:

“Apple, Banana, Guava, Lemon, Plum, Cherry, Blueberry”

Now if you opt for a regex search and type in:

a

Every instance of a will be highlighted. 

As always, this example is barely of any use, outside education purpose. What if I asked you to search for not only the letter ‘a’, but also instances of the letters ‘e’ and ‘b’? Simply use the following syntax:

[aeb]

But how does this work?

Remember the very first example we have earlier, organi[sz]e? According to the regex syntax, if you put a list of letters in between square braces ‘[ ]’, then all instances of every letter inside the square braces will be highlighted. There are hundreds of other things you can do with regex that match characters. Take this one to be just an example.


Regex contains word

If you want to search for words inside a given chunk of text, you’d be laughing at how easy it is. Assume that you want to search for the word Apple, all you’d do is type in the word Apple as the regex:

Apple

Now just as a quick tip. If you want to highlight all the words in the file, there’s a regex shortcut for that:

\w*

Regex not contain character

In order to highlight every other character than the one you specified in the regex pattern, you simply need to negate the search, by wrapping the character inside:

((?!character).)

You can also specify more than a single character, by dividing each with a ‘|’:

((?!character|character|character).)

For example, if I wanted to highlight all the characters other than A, B and N, I’d simply use this regex:

((?!A|B|N).)

Regex not contain word

What if you wanted to match all the words other than the one specified? Simple, we use the previous syntax one again, but we will replace the character with the word and add \w* at the end:

((?!word).)\w*

If you want to match whole lines only that does not contain the given word, you can do that by using this syntax:

^((?!word).)*$

Regex not contains string

Matching every other string that does not contain the given string is also the very same as we mentioned in the previous section:

((?!string).)*

Till now, we’ve only seen regex patterns containing stuff in general. It’s time to see how things are done in actual programming languages. Beware, it will not be as simple as you might’ve thought, so hold your seats tight!


Javascript string contains regex

In Javascript, in order to carry out a regex search, you first need to create a variable whose value is a regex pattern, enclosed between two backward forward slashes:

let regex = /pattern/;

Given that you have already created a string that contains some text:

let someText = "an apple tree";

You need to use the exec() method in Javascript in order to apply the regex to the given string:

let searchResult = regex.exec(someText);

If I wanted to search for the word apple in our string of text, the full code should look like this:

let regex = /apple/;
let someText = "an apple tree";
let searchResult = regex.exec(someText);

Remember that the exec() method will only return a single match of the string apple. If you have multiple instances of apple, it’ll only print out the first match. This is helpful when you want to clarify whether a given string contains the regex or not, and that is exactly what we want to do here.

You can then later print out the value of searchResult via console.log().


Python string contains regex

In Python, you need to import the re module in order to carry out regex searching;

import re

Now, you can use the search() function under re to do a regex search. This is the syntax:

searchResult = re.search("pattern", "text")

This time around, you won’t need to use forward slashes like in Javascript. For example, if I wanted to search for the same apple word in the string “an apple tree”, this is what I’d do:

text = "an apple tree"
regex = "apple"
searchResult = re.search(regex, text)

If you now print the value of searchResult:

print(searchResult)

You not only get a match as the output, but also some extra information you might find handy:

output:

<re.Match object; span=(3, 8), match='apple'>

C/C++ string contains regex

In C++11 and above, you can use the <regex> library to carry out regex searching:

#include <regex>

Now, first specify the regex you want to search for by using:

regex reg("pattern");

Afterwards, we will specify a string that we want to apply the search on, and then use the regex_search() function template to carry out the search on that string:

string myText = "an apple tree";
cout << regex_search(myText, reg) << endl;

The full code should look like this:

#include <iostream>
#include <string>
#include <regex>

int main() {
  regex reg("apple");
  string myText = "an apple tree";

cout << regex_search(myText, reg) << endl;

return 0;
}

Here, you will not receive an output of the word ‘apple‘ itself if it matched. You’ll instead get an output of 1 or 0. If the output is 1, then a match was found. If the output is 0, then no matches were found.


C# string contains regex

In C#, you need to use the Regex Class which will allow you to use regular expressions:

using System.Text.RegularExpressions;

Afterwards, you can use the Regex.isMatch() method and pass it to an if statement which will allow you to check whether a match is found or not, and give back an output based on the result. This is the syntax you’d use:

Regex.IsMatch(“string”, “regex”)

When writing the regex, you need to make sure that you append a @ in the front, as it will allow C# to recognise it as a regular expression:

string regexPattern = @"pattern";

Once again, if I want to check if the word apple is found inside the string “an apple tree” in C#, this is the full code for it:

using System;
using System.Text.RegularExpressions;

class Regex_Test
{
  static void Main(string[] args)
    {
      string someText = "an apple tree”;
      string regexPattern = @"\bapple\b”;

      if (Regex.IsMatch(someText, regexPattern))
    {
       Console.WriteLine("Match is found”);
    }
     else
    {
      Console.WriteLine("No match is found”);
    }
  }
}

Remember that the IsMatch() returns a boolean value, which is why we passed it to an if statement.