我试图从一组字符串中提取以下内容:
string1: copyright © 2014 domain.com. All rights reserved.
string2: copyright © 2012 - 2014 domain.net. All rights reserved.
string3: copyright © 2012 - 2014 domain. All rights reserved.
我的reg exp获得©和域名之后的年份(或年份范围):
\©([0-9]*)(.*?)(\.com|.net|.org|.co)
However it is returning
"","2014 domain.com"
"","2012 - 2014 domain.net"
null
I am expecting this:
"2014", "domain.com"
"2012 - 2014", "domain.net"
"2012 - 2014", "domain"
我对regexp很新,任何帮助或方向都会非常感激。
谢谢,
答案 0 :(得分:2)
答案 1 :(得分:1)
我认为Avinash的解决方案很好,但我宁愿做
([0-9]{4}(?: - [0-9]+)?) (.*?)(?=\. All)
也会抓住这个:
string4: copyright © 2012 - 2014 sub.domain.net. All rights reserved.
答案 2 :(得分:0)
您可以将此正则表达式与匹配的组#1(年份范围)和#2(域名)一起使用:
/© *(\d{4}(?: *- *\d{4})?) *(\S+?(?:\.(?:com|net|org|co))?)(?=\.)/