cpp check if the string is a valid domain name? - c++

as title mentioned, is there a quick way to do that? I dont need a solid solution, anything that can differentiate, for example:
http://asdasd/
is not a valid domain name, where
http://asd.asdasd.asd
is a valid domain name.
I tried to search the solution, the closest(simple) solution is this: in python
But thats for python, I need to do in c++. Any help?
Can it be done by using "string manipulation" only? Like, substring?

I believe this can be done with libcurl.

Baring the fact that http://... is not a domain name but a URL, and that asdasd is as valid domain name if setup as a search domain (such as on local net), then purely checking for the string syntax can be done with a simple set of strncmp, strchr and strstr commands
char *str = "http://abd.xxx";
bool valid = strncmp(str,"http://",7) && str[7] && strchr(str+7,'.');
This should check that the string starts with http:// AND that there is more after the http:// and that the more after that contains a dot -- if you also want to handle where the URL contains an actual path like http://expample.com/mypath.txt, then the example become more complex, but you didn't specify if that was needed.
Alternatively, you can use regex and the pattern which you have from the python answer you point to yourself

Related

Extract domain name from string

I'm looking how to extract a domain name from a URL in a column in my PowerBI report.
I tried to use that formula:
DOMAIN = LEFT([URL],FIND("/",[URL],9)-1)
But it says
The search Text provided to function 'FIND' could not be found in the given text.
Thanks for your help.
In DAX it would be SEARCH which has the same syntax as the Excel FIND function:
SEARCH(<find_text>, <within_text>[, [<start_num>][, <NotFoundValue>]])
FIND(find_text, within_text, [start_num])
So it will be
DOMAIN = LEFT([URL],SEARCH("/",[URL],9)-1)
Update: there is a find function in DAX, I didn't realise it, always used Search! Search supports wildcards find doesn't.
Your formula depends on the URL containing a "/" after the http section. I think it's failing because, there is no "/" after the first few characters. So, you may have to improvise, based on the URL you see. For example, if the domain ends with .com, you could use:
DOMAIN = LEFT([URL],FIND(".com",[URL],9)+3)
The type of URL you have becomes important here. Hope this helps.

Regex Expression to replace email address domain, for users email address

I am trying to solve an email domain co-existence problem with Exchange online. Basically i need it so when a message is sent to one tenant (domain.com) and forwarded to another tenant (newdomain.com) - that the To and/or CC headers are replaced with the endpoint (newdomain.com) email addresses before they are delivered to the final destination.
For Example:
1) Gmail (or any) user sends and email to sally.sue#domain.com, MX is looked up for that domain, it is delivered to the Office 365 Tenant for domain.com
2) That same office 365 tenant, is set to forward emails to sally.sue#newdomain.com (different tenant)
3) When the message arrives to sally sue at newdomain.com and she hits "Reply All" the original sender AND her (sally.sue#domain.com) are added to the To: line in the email.
The way to fix that is to use Header Replacement with Proofpoint, which as mentioned below works on a single users basis. The entire question below is me trying to get it to work using RegEx (As thats the only solution) for a large number of users.
I need to convert the following users email address:
username#domain.com to username#newdomain.com
This has to be done using ProofPoint which is a cloud hosted MTA. They have been able to provide some sort of an answer but its not working.
Proofpoint support has suggested using this:
Header Name : To
Find Value : domain\.com$
Replace : newdomain\.com$ or just newdomain.com
Neither of the above work. In both cases the values are just completely ignored.
This seems to find the values:
Header Name : To
Find Value : \b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b
Replace : $1#fake.com
But the above simply and only replaces the To: line (in the email) with the literal string: $1#fake.com
I would also need to be able to find lowercase and numbers in email addresses as well. i believe the above example only finds caps.
I need it do the following:
Header Name : To
Find Value : \b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b (find users email address, domain)
Replace : user.name#newdomain.com
This is for a large number of users so there is no way to manually update or create separate rules for each user.
If i do create a individual rule, then it works as expected but as stated that requires manually typing out each user To: address And their new desired To: address.
This solution here almost worked: Regex to replace email address domains?
I have a couple of observations from general experience, although I have not worked with Office365 specifically.
First, a regex used for replacement usually needs to have a "capture group". This is often expressed with parentheses, as in:
match : \b([A-Z0-9._%-]+)#domain.com$
replacement : $1#newdomain.com
The idea is that the $1 in the replacement pattern is replaced with whatever was found within the () in the matching pattern.
Note that some regex engines use a different symbol for the replacement, so it might be \1#newdomain.com or some such. Note also that some regex engines need the parentheses escaped, so the matching pattern might be something like \b\([A-Z0-9._%-]+\)#domain.com$
Second, if you want to include - inside a "character class" set (that is, inside square brackets []), then the - should be first; otherwise it's ambiguous because - is also used for a range of characters. The regex engine in question might not care, but I suggest writing your matching pattern as:
\b([-A-Z0-9._%]+)#domain.com$
This way, the first - is unambiguously itself, because there is nothing before it to indicate the start of a range.
Third, for lowercase letters, it's easiest to just expand your character class set to include them, like so:
[-A-Za-z0-9._%]

Check a Location/Path

i'm really new on C++ and i want to try a little bit C++. Normally i'm come from Java/PHP.
I have a String like;
std::string location = "file:///C:/Program Files (x86)/Demo/";
or
std::string location = "http://www.example.com/site.php";
How i can check:
a has location the domain www.example.com or example1.com
b starts the domain with http:// or https://
In Java or PHP i would take Regular Expression. But simply no idea how to start in C++.
My first things was to check http://:
std::string location = "";
if (strncmp(location.c_str(), "http://", 7)) {
/* yepp */
} else {
/* nope */
}
But that won't work.
I hope you can help me.
I'll attack your question in three ways, from most to least specific.
1 - Instead of reinventing the wheel, you can opt for suggestions already given here on Stack Overflow:
Easy way to parse a url in C++ cross platform?
2 - Regexps are indeed fully supported in C++. You might refer to the following as a start:
http://www.cplusplus.com/reference/regex/regex_search/
3 - In general, it is not advisable to utilize C-style functions such as strncmp to compare strings. The std::string class has several substring search functions that you'd best be using. The most basic of them is the following:
http://www.cplusplus.com/reference/string/string/find/
Hope this helps you get on the right track regardless of how you choose to proceed.

How to create a regex to check whether a set of words exists in a given string?

How can I write a regex to check if a set of words exist in a given string?
For example, I would like to check if a domain name contains "yahoo.com" at the end of it.
'answers.yahoo.com', would be valid.
'yahoo.com.answers', would be wrong. 'yahoo.com' must come in the end.
I got a hint from somewhere that it might be something like this.
"/^[^yahoo.com]$/"
But I am totally new to regex. So please help with this one, then I can learn further.
When asking regex questions, always specify the language or application, too!
From your history it looks like JavaScript / jQuery is most likely.
Anyway, to test that a string ends in "yahoo.com" use /.*yahoo\.com$/i
In JS code:
if (/.*yahoo\.com$/i.test (YOUR_STR) ) {
//-- It's good.
}
To test whether a set of words has at least one match, use:
/word_one|word_two|word_three/
To limit matches to just the most-common, legal sub-domains, ending with "yahoo.com", use:
/^(\w+\.)+yahoo\.com$/
(As a crude, first pass)
For other permutations, please clarify the question.

What's the best way to validate a user-entered URL in a Cocoa application?

I am trying to build a homebrew web brower to get more proficient at Cocoa. I need a good way to validate whether the user has entered a valid URL. I have tried some regular expressions but NSString has some interesting quirks and doesn't like some of the back-quoting that most regular expressions I've seen use.
You could start with the + (id)URLWithString:(NSString *)URLString method of NSURL, which returns nil if the string is malformed.
If you need further validation, you can use the baseURL, host, parameterString, path, etc methods to give you particular components of the URL, which you can then evaluate in whatever way you see fit.
I've found that it is possible to enter some URLs that seem to be OK but are rejected by the NSURL creation methods. So we have a method to escape the string first to make sure it's in a good format. Here is the meat of it:
NSString *escapedURLString =
NSMakeCollectable(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)URLString,
(CFStringRef)#"%+#", // Characters to leave unescaped
NULL,
kCFStringEncodingUTF8));