第一场比赛时`String#gsub`

时间:2015-11-16 11:31:50

标签: ruby regex gsub

我有一个字符串如下:

string = "request=hxxp://abc.com/user=username?pass=password"

我需要将等号"="的第一个实例替换为"***"。我希望能实现:

string = "request***hxxp://abc.com/user=username?pass=password"

我测试了以下,但没有运气:

string = string.gsub(/^[^=]+(=)/,'***') # => "***hxxp://..."
string = string.gsub(/((?<=())=)/,'***') # => all "=" replaced with "***"

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

使用sub。这应该只替换Int32 nn = 4308; VISITE v = VISITEs.FirstOrDefault(a => a.VINUM == nn); DateTime d = (DateTime) v.DATEVIS; Int32 p = (Int32) v.PANUM; // First Select var numberGroups = from n in VISRUBs.Where(a => a.VISANA.VISITE.DATEVIS <= d && a.VISANA.VISITE.PANUM == p) group n by n.RUBRIQUE into g select new { RemainderCHAPLIB = g.Key.ANALYSE.CHAPITRE.LIBELLE, RemainderLIB = g.Key.LIBELLE, RemainderRUNUM = g.Key.RUNUM, vals = from vlist in g.OrderByDescending(a => a.VISANA.VISITE.DATEVIS) select vlist.VALEUR }; //Second Select var lst = from n in numberGroups. ToList().Where(a => a.RemainderLIB != null) select new { RemainderCHAPLIB = n.RemainderCHAPLIB, RemainderLIB = n.RemainderLIB, RemainderRUNUM = n.RemainderRUNUM, VAL = n.vals.FirstOrDefault(), ANT = n.vals.Skip(1).FirstOrDefault() }; 的第一次出现。

=

对于gsub,它必须是

string.sub(/=/, "***")