My goal is to remove names and numbers from a string in Java. Example: Input - & gt; Output
1234 - & gt; Numbers: [1234], Name: []
1234, 34,234 - & gt; Numbers: [1234, 34, 234], Name: []
12, foo, 123 - & gt; Numbers: [12, 123], name: [foo]
foo3,1234,4bar, 12,12foo34 - & gt; Numbers: [1234, 12], name: [foo3, 4bar, 12foo34]
foo, bar - & gt; - & gt; Number: [], name: [foo, bar]
I
[^,] + (,?!, +) * parts of the string, but I do not know how to get only numbers or names (name may include in the name - for example) Thanks
Here is a regex-only solution:
(?: (\ D +) | ([^,] +)) (? =, | $)
The first group gets the code
(\ d +) , the second group
([^,] +) catches the rest. A group is a comma or line
(? = = | $) . Should be done after the end of.
A quick demo:
pattern P = Pattern.compile ("(:: (\\ d +) | ([^,] +)) (? , | $) "); Matcher m = p.matcher ("foo3,1234,4bar, 12,12foo34"); While (m.find ()) {System.out.println (m.group (1)! = Null? "Number:" + m.group (1): "non-numbers:" + m.group (2) ); }
Output:
non-numbers: foo3 number: 1234 non-numbers: 4bar number: 12 Non-numbers: 12foo34
No comments:
Post a Comment