1

I am trying to match the first word after a specific string using regex and have been struggling to make all instances work properly. Let's say I have 2 strings:

Site Code: foobar

Site Code: hello-world

Now, using (?<=\bSite Code:\s)(\w+) returns foobar which is correct, but only returns hello when I need hello-world instead.

So, I changed my expression to (?<=\bSite Code:\s)(\w+)(-\w+) in order to pickup the hyphenated words, but now it is ignoring the non-hyphenated words.

Is there a way to get both foobar and hello-world from the same expression?

4

2 回答 2

2

尝试这个(?<=\bSite Code:\s)(\S+)

如果没有关于要求的更多细节,很难给出准确的答案。上面的正则表达式应该匹配特定字符串“站点代码:”之后的所有非空白字符。

于 2013-10-18T16:13:57.897 回答
1

试试这个正则表达式:

(?<=\bSite Code:\s)([-\w]+)

#\w only include the range [A-Za-z0-9_] in ascii mode.
于 2013-10-18T16:05:02.980 回答