开发者

Regex to get only numbers and the minus sign (-)

I was looking for the Regular Expression to get only numbers and t开发者_如何学运维he symbol £ and also -. Nothing else should be accepted.

Because I have the strings £24 -£ 57 only.

And I need to get only £24 - £57

Thanks


To match &, numbers and -:

[£0-9\-]

So the input £24 -£ 57 will match:

1.  £
2.  2
3.  4
4.  -
5.  £
6.  5
7.  7


Something like this?

\£([0-9]+)

By the way you should accept some previous questions, and read your post before pressing submit.


Escape the pound symbol with \


Have you tried /[£\d-]*/? That does what you described. However, I have a feeling it's not what you actually want. There should be only one £ for each number group, and only at the beginning of the group. Hyphens go at the beginning as well, and are the only optional part of the pattern. Since there's whitespace in your sample, I think I'd use something like this:

/(\s*(-\s*)?£\d+)*/

I don't think the £ symbol needs to be escaped; at least, it didn't when I tested it.


try this: \£\d{2}-\£\d{2}

will extract this pattern(£24-£32) from any string.


did you try with -?£\d+ there should be nothing special about the £

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜