开发者

Replae last occurrence of specific digit inside square brackets

I need to find last digit from string and I am stuck.

Could you please tell me how do I replace last 0 from following string:

something[0].mor开发者_如何学JAVAesomething[0].evenmoresomething


This would get you the last digit... also known as a 'negative lookahead'.

\d(?!.*\d)

If you're doing this in .NET, to replace the last character you'd do something like:

string source = "something[0].moresomething[0].evenmoresomething";
string regEx = @"\d(?!.*\d)";
string result = Regex.Replace(source, regEx, "2");

'result' would now contain the string "something[0].moresomething[2].evenmoresomething"


Try this regex:

(?<=\[)0(?=\])(?!.*?\[0\])

It will match last 0 in []


You can do this

\d(\D*)$

and replace with whatever you want and $1

This will work if your number is really the last one in the string/row. (See Kirills example in the comment).

See it here on Regexr

$1 is here the content of the first capturing group. that means the content of the (), i.e. everything thats following the digit you want to replace.

\d is a digit

\D is not a digit

$ is the end of the string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜