简体   繁体   中英

Regex for matching domain and ip address

对于我的Rails应用程序,我想要一个正则表达式,它将匹配域和IP地址。

You could use something like this:

\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b

Taken from here .

EDIT: For domain names, you could use something like so: ^http://(.*?)/?.* . However, it would help if you could post some domain name examples. The regex I have posted should match url which starts with http:// and it will then throw the domain name, such as google.com in a group which you can then access later.

如果您使用的是网址,并尝试匹配主机部分,

/^http:\/\/([^\/])\//

Whenever I need a regex for something, I first go to the Regular Expressions Library . It is there that I found this one that looks quite reasonable:

^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$

I would use a separate regex for IP addresses and ask if the string matches either instead of trying to make both patterns fit a single matcher.

^

I would say you need to be more specific, I also like to use http://www.rubular.com/ to develop my regexs. It has a reference key under the editor so you can play with it. You can have fun and get your work done at the same time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM