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+&@#/%=~_|])"
Explanation
/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 https://www.codespeedy.com/"; 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; else cout << "Url is not detected in the given string" << endl; return 0; }
Output
Url detected in the given string: https://www.codespeedy.com/
Complexity Analysis
Time Complexity: O(String Size)
Space Complexity: O(1)
Similar posts
Leave a Reply