用逗号分隔数据
我正在学习正则表达式。完全是新手:P
我想将数字与下面的数据分开,这些数据仅用逗号分隔
test
t,b
45,49
31,34,38,34,56,23,,,,3,23,23653,3875,3.7,8.5,2.5,7.8,2., 6 6 6 6 ,
,
.
.,/;,jm.m.,,n ,sdsd, 3,2m54,2 4,2m,ar ,SSD A,,B,4D,CE,S4,D,2343ES,SD
假设我从表单文本字段获取上述数据。现在我只想读取以逗号分隔的数字的数据
解决方案应该是[string]
45,49,31,34,38,34,56,23,3,23,23653,3875
所有其他数据都应该被跳过。 我尝试了这样的 ^[0-9]+\,$
但它也从 3.7 中选择 7,从 8.5 中选择 5,等等......
任何人都可以帮我解决这个问题!
I am learning RegEx. completely a newbie :P
I wanted to separate numbers from the below data, which are separated by comma only
test
t,b
45,49
31,34,38,34,56,23,,,,3,23,23653,3875,3.7,8.5,2.5,7.8,2., 6 6 6 6 ,
,
.
.,/;,jm.m.,,n ,sdsd, 3,2m54,2 4,2m,ar ,SSD A,,B,4D,CE,S4,D,2343ES,SD
Suppose I am getting the above data from Form text field. Now I want to read the data only which are numbers seperated by comma
Solution should be[string]
45,49,31,34,38,34,56,23,3,23,23653,3875
all other data should be skipped.
I tried something like this ^[0-9]+\,$
But it's also selecting 7 from 3.7, and 5 from 8.5, etc.....
Can anyone help me out in solving this!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能想到的最短的解决方案是用空字符串替换不是由逗号分隔的一组数字的任何内容。因此,您可以执行
s.replaceAll("[^0-9]*,", ",")
如果其中有随机换行符,您可能需要添加s .replaceAll("\n", ",")
.然后在这些转换之后,您可以按照建议进行操作并用逗号分隔。The shortest solution i could come up with would be to replace anything that isn't a set of numbers separated by commas with the empty string. So you could do
s.replaceAll("[^0-9]*,", ",")
If you have random newlines in there, you will probably want to add in as.replaceAll("\n", ",")
. Then after those transformations, you can just do as suggested and split on commas.这个表达式将为您提供所需的所有数字(只有数字,没有逗号)。
请参阅 grep 示例:
this experssion will give you all numbers you need (only numbers, no commas).
see the grep example:
假设您已经以逗号分隔并尝试检查获得的元素是否是数字,请使用以下表达式:
^\d+(?:\.\d+)?$
,这意味着:“必须开始数字后面可能有一个点和至少一个数字”。这将匹配
31
以及7.8
,但不匹配2.
、6 6 6 6
或2分54秒。
下面是对该表达式的部分解释:
^
表示:匹配必须从第一个字符开始$
表示:匹配必须在最后一个字符结束,因此两者一起表示整个字符串必须匹配\d+
表示:一个或多个数字(?: ... )
是一个非捕获组,允许应用?
量词\.
表示:文字dot(?:\.\d+)?
表示:零次或一次出现一个点,后跟至少一位数字编辑:如果您只需要整数,只需删除该组:
^\d+$
->整个输入必须是一位或多位数字。编辑 2:
如果您可以在输入字符串前面添加逗号(请参阅编辑 4),您应该能够使用此正则表达式来获取所有数字:(?<=, )\s*(\d+(?:\.\d+)?)\s*(?=,)
(仅整数需要删除(?:\.\d+)?
部分)。该表达式获取两个逗号之间的所有数字,逗号和数字之间可能有空格,并将数字捕获到一个组中。这应该可以防止匹配
6 6 6 6
或2m54
。然后只需迭代匹配即可获得所有组。编辑 3:这是输入字符串的示例。
编辑4:实际上,您不需要添加逗号,只需使用以下表达式:
开头和结尾的组意味着匹配必须跟在输入的开头、逗号或换行符之后,然后是结尾输入,逗号或换行符。
Assuming you are already splitting at commas and try to check whether the elements you get are numbers, use this expression:
^\d+(?:\.\d+)?$
, which means: "must begin with digits potentially followed by a dot and at least one more digit".This would match
31
as well as7.8
, but not2.
,6 6 6 6
or2m54
.Here's a part by part explanation of that expression:
^
means: matches must start at the first character$
means: matches must end at the last character, so both together mean the entire string must match\d+
means: one or more digits(?: ... )
is a non-capturing group allowing to apply the?
quantifier\.
means: the literal dot(?:\.\d+)?
thus means: zero or one occurences of a dot followed by at least one digitEdit: if you only want integer numbers, just remove the group:
^\d+$
-> entire input must be one or more digits.Edit 2:
If you can prepend and append a comma to the input string(see Edit 4), you should be able to use this regex for getting all numbers:(?<=,)\s*(\d+(?:\.\d+)?)\s*(?=,)
(integers only would require you to remove the(?:\.\d+)?
part).That expression gets all numbers between two commas with possible whitespace between the commas and the number and catches the number into a group. This should prevent matches of
6 6 6 6
or2m54
. Then just iterate over the matches to get all the groups.Edit 3: Here's an example with your input string.
Edit 4: actually, you don't need to add commas, just use this expression:
The groups at the start and end mean the match must follow the start of the input, a comma or a line break and be followed by the end of the input, a comma or a line break.