Remove URL from a string in C++

In this tutorial, one can get to know how to use regular expression and how to use it for removing a URL from 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+&@#/%=~_|])"


Please refer to Detect URL in a string in C++, which makes it easy to understand this code.


I am checking each word in the given string if it’s a URL or not. So if it’s a URL, then I am not adding it to the resultant string. That’s it!!

Let’s see the C++ code: Remove URL from a string

#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 = "This is testing string to remove a url";
    string word, url;
    stringstream ss(test);

    while(ss >> word){
        if(regex_match(word, regUrl))
        url += word + " ";

    cout << "Modified the given string to: " << url << endl;
    return 0;


Modified the given string to: This is testing string to remove a url


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 *