[英]How to can I write a regular expression to match free-text followed by whitespace?
Sorry for the bad title but it's a little complicated to get across in one line. 对不起,标题不好,但要排成一排有点麻烦。
My data will look something like the following lines (each evaluated separately): 我的数据类似于以下几行(每行分别进行评估):
JOHNNY BE GOOD 2.55
Rubber Ducky (2012) 123.71
International: INT'L 29.12
I'm trying to split them as follows: 我正在尝试将它们拆分如下:
[JOHNNY BE GOOD][ ][2.55]
[Rubber Ducky (2012)][ ][123.71]
[International: INT'L][ ][29.12]
That is, from left-to-right, as much text as possible (including white-space), then any trailing white-space, then a decimal number with 2 decimal places. 也就是说,从左到右,尽可能多的文本(包括空格),然后是任何尾随空格,然后是带有2个小数位的十进制数字。
So far I have the following expression but my white-space is captured with the text not by itself: 到目前为止,我具有以下表达式,但是我的空白不是用文本本身捕获的:
/(.*)(\s+)([0-9]+\.[0-9]{2})/
What am I doing wrong? 我究竟做错了什么?
Thanks! 谢谢!
Your (.*)
is being greedy, ie, is taking all the characters it can. 您的
(.*)
贪婪,即正在使用所有可能的字符。 Try using the laziness operator ?
尝试使用惰性运算符
?
: :
/(.*?)(\s+)([0-9]+\.[0-9]{2})/
or well: 还是:
/([^\s]*)(\s+)([0-9]+\.[0-9]{2})/
You were pretty close, but you're using the .
您已经很接近了,但是您正在使用
.
character which will match everything including white space. 匹配所有内容(包括空格)的字符。 Simply add
[^\\s]
from the first captured group to your regex so that it'll look something like this 只需将第一个捕获的组中的
[^\\s]
添加到您的正则表达式中,使其看起来像这样
(.*[^\\s])(\\s+)([0-9]+\\.[0-9]{2})
http://gskinner.com/RegExr/?2vo43 http://gskinner.com/RegExr/?2vo43
I'd recommend changing the (\\s+)
part to (\\s{2,})
which makes sure that there's repeating whitespace, not just a single character. 我建议将
(\\s+)
部分更改为(\\s{2,})
,以确保存在重复的空格,而不仅仅是单个字符。
You also might want to throw in some line start/end anchors to prevent wildcards from "eating" too much data. 您可能还想添加一些行开始/结束锚点,以防止通配符“吃掉”太多数据。 To help balance this out and have them match at newlines, add the
m
flag to your regular expression. 为了平衡平衡并使它们在换行符处匹配,请将
m
标志添加到正则表达式中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.