Detect URL in a string in C++

In this tutorial, one can get to know how to use regular expression and how to use it for detecting a URL in a given string in C++.

Regular Expressions

Regular Expressions are a sequence of characters used to match the pattern within a string.

The regular expression for a URL is:

"\\b((?:https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:, .;]*[-a-zA-Z0-9+&@#/%=~_|])"


/b : matches any word boundary
(abc) – it matches the characters abc in that exact order.
? – may or may not match
ftp | file – its boolean ‘or’ operation
[-a-zA-Z0-9+&@#/%?=~_|!:, .;]* – any possible combination of characters
[-a-zA-Z0-9+&@#/%=~_|] – it should contain a character compulsorily from this set because it can’t be empty

Let’s see the code

#include <iostream>
#include <bits/stdc++.h>
#include <regex>

using namespace std;

int main(){
    regex regUrl("\\b((?:https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:, .;]*[-a-zA-Z0-9+&@#/%=~_|])");
    string test = "A good resource for your programming problems is";
    string word, url;
    stringstream ss(test);

    while(ss >> word)
        if(regex_match(word, regUrl)) // checking if given string matches the subsequence with regular expression
            url = word;

    if (url.size())
        cout << "Url detected in the given string: " << url << endl;
        cout << "Url is not detected in the given string" << endl;

    return 0;


Url detected in the given string:


Complexity Analysis

Time Complexity: O(String Size)
Space Complexity: O(1)

Similar posts

Validate Email-Id using regex in C++

Leave a Reply

Your email address will not be published. Required fields are marked *