开发者

Replae last occurrence of specific digit inside square brackets

开发者 https://www.devze.com 2023-04-05 02:15 出处:网络
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:

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

精彩评论

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