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+&@#/%=~_|])"

Explanation

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

Approach

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<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 = "This is testing string to remove a url https://www.codespeedy.com/";
    string word, url;
    stringstream ss(test);

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

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

Output

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.