cpp 检查字符串是否是有效的域名?
正如标题所提到的,有没有一种快速的方法可以做到这一点?我不需要一个可靠的解决方案,任何可以区分的东西,例如:
http://asdasd/
不是一个有效的域名,哪里
http://asd.asdasd.asd
是一个有效的域名。
我尝试搜索解决方案,最接近(简单)的解决方案是: 在Python中
但这就是Python的,我需要在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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这可以通过
libcurl
来完成。I believe this can be done with
libcurl
.事实是,
http://...
不是域名,而是 URL,并且asdasd
是有效域名,如果设置为搜索域(例如在本地网络上),然后可以使用一组简单的strncmp
、strchr
和strstr 来纯粹检查字符串语法
命令这应该检查字符串是否以 http:// 开头并且存在http:// 之后的 more 以及之后的 more 包含一个点 - 如果您还想处理 URL 包含实际路径的位置,例如
http://expample.com/mypath.txt
,那么示例变得更加复杂,但您没有指定是否需要这样做。或者,您可以使用正则表达式以及您从指向的python答案中获得的模式你自己
Baring the fact that
http://...
is not a domain name but a URL, and thatasdasd
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 ofstrncmp
,strchr
andstrstr
commandsThis 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