使用正则表达式获取必要的值 [英] Using regex to pick up the necessary values

查看:82
本文介绍了使用正则表达式获取必要的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一行:

I have the following line:

5 CTS-PHD-CAM-USB= SPARE ONLY - PRECISIONHD USB NOCLASS-EU 1 1 192.03 192.03 USD



我使用正则表达式从行中获取以下值:


I used regex to pick up the following values from the line:

\d+ ---------> 5

[-A-Za-z\d=]+ ---------> CTS-PHD-CAM-USB= 

[A-Za-z\s-] ---------> SPARE ONLY - PRECISIONHD USB.





但现在的问题不仅在于它只是备用SPARE - PRECISIONHD USB它还能拿起NOCLASS -EU。

我怎么才能只拿这个SPARE - PRECISIONHD USB而不是整个SPARE - PRECISIONHD USB NOCLASS-EU?



But now the problem is instead of it just picking up SPARE ONLY - PRECISIONHD USB it also picks up NOCLASS-EU.
How can I pick up just this SPARE ONLY - PRECISIONHD USB instead of the whole SPARE ONLY - PRECISIONHD USB NOCLASS-EU?

推荐答案

防止这种情况的唯一方法是对接受的字符数添加限制。可能你正在使用

The only way to prevent that would be to add a limit to the number of characters accepted. Possibly you are using
[A-Za-z\s-]*

你想改为:

And you want to change to:

[A-Za-z\s-]{1,28}


这篇关于使用正则表达式获取必要的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆