查看完整版本 : regx問題

sinson123 2017-12-31 10:03 AM

regx問題

諗唔到個regx pattern

我想將個domain name replace咗佢
programme.tvb.com->*.tvb.com
tvb.com->*.tvb.com

我試過 ^\w*.  但第二個就變成*.com
個pattern應該係點呢

fitcat07 2017-12-31 12:55 PM

In vim:[code]s:\(\w\+\.\)*tvb\.com:\*\.tvb\.com[/code]s means substitute
: is the separator

fitcat07 2017-12-31 01:00 PM

如果唔一定tvb.com,要用groupings and back reference:[code]s:\(\w\+\.\)\{-}\(\w\+\.\)com:\*\.\2com[/code]{-} means match zero or more as minimum as possible
\2 means the second group

sinson123 2018-1-2 09:35 AM

thx, 我用係.net上,試過,搵唔到有match....

fitcat07 2018-1-2 12:10 PM

[url]https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions[/url]

form5 2018-1-2 08:00 PM

var regex = new Regex(@"\S*\.*tvb.com");

Console.WriteLine(regex.Replace(" 123 programme.tvb.com", "*.tvb.com")); //123 *.tvb.com
Console.WriteLine(regex.Replace(" xyz *.tvb.com", "*.tvb.com")); //xyz *.tvb.com

form5 2018-1-2 08:08 PM

Console.WriteLine(regex.Replace("hello tvb.com", "*.tvb.com")); //hello *.tvb.com

form5 2018-1-2 11:43 PM

試左下js
https://codepen.io/anon/pen/YYQvxV
頁: [1]
查看完整版本: regx問題