A
A
andreycha2011-10-22 14:57:25
Java
andreycha, 2011-10-22 14:57:25

Help with regexp (Java)

There is some set of strings containing values ​​separated by commas. Among them, you need to filter out those whose first value is either any number, or "NA".

Valid strings:
2,whatever
NA,whatever
45345,whatever

Invalid strings:
,whatever
bla-bla,whatever
a2,whatever
NANA,whatever

Okay, I wrote the expression:

(?m)^(\d+|NA{1}),.*

Everything works great. However, it later turned out that terminal characters (\t, \r, \n etc.) can occur in the whatever part. Modified the expression:

(?m)^(\d+|NA{1}),(.|\s)* (as an option - (?s)(?m)^(\d+|NA{1}),.* )

And it was not there, now all the lines, including invalid ones, began to satisfy him.

Tell me please, where did I go wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Optik, 2011-10-22
@andreycha

(?<!\S)(?:NA|\d{1,}),(?:.|\s)+?(?=(?:\n.+,)|$)
Try this if you are feeding all lines at once and not one at a time. Well, the line separator was taken \n, if you mind that.

S
soider, 2011-10-22
@soider

Maybe you should use some library to work with csv?

K
Konstantin, 2011-10-22
@Norraxx

you give it .* and (.|\s)*, it eats everything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question