逗号分隔数据 [英] Separate Data by Comma

查看:77
本文介绍了逗号分隔数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习RegEx。完全是新手:P

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

解决方案应该是[string]

Solution should be[string]

45,49,31,34,38,34,56,23,3,23,23653,3875

所有其他数据都应跳过。
我尝试了类似^ [0-9] + \,$

all other data should be skipped. I tried something like this ^[0-9]+\,$

的方法,但是它也在3.7中选择了7,在8.5中选择了5,依此类推。 ....

But it's also selecting 7 from 3.7, and 5 from 8.5, etc.....

有人可以帮我解决这个问题吗!

Can anyone help me out in solving this!!

推荐答案

假设您已经在逗号之间分割,并尝试检查获取的元素是否为数字,请使用以下表达式: ^ \d +(?: \.\d +)?$ ,这意味着:必须以数字开头,后接一个点和至少一个数字。

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".

这将匹配 31 7.8 ,但不是 2。 6 6 6 6 2m54

This would match 31 as well as 7.8, but not 2., 6 6 6 6 or 2m54.

下面是该表达式的部分解释:

Here's a part by part explanation of that expression:


  • ^ 表示:比赛必须从第一个字符开始

  • $ 表示:比赛必须在最后一个字符处结束,因此两者都意味着整个字符串必须匹配

  • \d + 表示:一个或多个数字

  • (?: ...)是一个非捕获组,允许应用量词

  • \。表示:文字点

  • (?: \.\d +)?的意思是:点的零次或一次出现,后跟至少一位数字

  • ^ 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 digit

编辑:如果只需要整数,则删除组: ^ \d + $ ->整个输入必须为1或

if you only want integer numbers, just remove the group: ^\d+$ -> entire input must be one or more digits.

编辑2:如果可以添加和添加输入字符串的逗号(请参见编辑4),您应该可以使用此正则表达式获取所有数字:(?< =,)\s *(\d + (?:\.\d +)?)\s *(?=,)(仅整数会要求您删除(?: \.\ \d +)?部分)。

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).

该表达式获取两个逗号之间的所有数字,并且逗号和数字之间可能存在空格,并将该数字分组。这样可以防止匹配 6 6 6 6 2m54 。然后,只需遍历所有匹配项即可。

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 or 2m54. Then just iterate over the matches to get all the groups.

编辑3:下面是输入字符串的示例。

Edit 3: Here's an example with your input string.

String input = "test\n" +
        "t,b\n" +
        "45,49\n" +
        "31,34,38,34,56,23,,,,3,23,23653,3875,3.7,8.5,2.5,7.8,2., 6 6 6 6 ,\n" +
        ",\n" +
        ".\n" +
        ".,/;,jm.m.,,n ,sdsd, 3,2m54,2 4,2m,ar ,SSD A,,B,4D,CE,S4,D,2343ES,SD\n";

Pattern p = Pattern.compile( "(?<=,|\\n)\\s*(\\d+(?:\\.\\d+)?)\\s*(?=,|\\n)" );    

Matcher m = p.matcher( input );

List<String> numbers = new ArrayList<String>();

while(m.find())
{
  numbers.add( m.group( 1 ) );
}

System.out.println(Arrays.toString( numbers.toArray() ));

//prints: [45, 49, 31, 34, 38, 34, 56, 23, 3, 23, 23653, 3875, 3.7, 8.5, 2.5, 7.8, 3]
//removing the faction group: [45, 49, 31, 34, 38, 34, 56, 23, 3, 23, 23653, 3875, 3]

编辑4:实际上,您不需要添加逗号,只需使用以下表达式:

Edit 4: actually, you don't need to add commas, just use this expression:

`(?<=,|\n|^)\s*(\d+)\s*(?=,|\n|$)`

开头和结尾处的组表示匹配必须跟随输入的开头,逗号或换行符,以及输入的末尾是逗号或换行符。

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.

这篇关于逗号分隔数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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