Regular expression to extract number with sign between quotes

一世执手 提交于 2021-02-19 07:43:26

问题


I'm using Unity 5.3.4 and using C#. I was wondering if it would be possible to extract with regular expressions the value in this string, I do need the sign, as now is negative but it doesn't have to be that way:

string str = " "rssi": "-56", "

I do know that to use regular expression in unity it would be this way:

str = Regex.Replace(str, "*regular expression*", String.Empty);

I was wondering what would be a proper regular expression. I've been trying some in http://www.regexr.com/ But with no luck. I'll keep trying.


回答1:


You can use following regular expresstion :[-+]?\d+

Meaning: get the one or more digits may be followed by sign.

Demo and Explaination




回答2:


Just found the solution:

Regex rgxNumber = new Regex("([-+]{0,1}[0-9]+)");
Match mNumber = rgxNumber.Match(str);

string result = mNumber.Groups[1].Value;

Hope it helps someone else.



来源:https://stackoverflow.com/questions/38821550/regular-expression-to-extract-number-with-sign-between-quotes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!