0

I am trying to validate a string in a 'iterative way' and all my tryouts just fail! I find it a bit complicated and i'm guessing maybe you could teach me how to do it right. I assume that most of you will suggest me to use regex patterns but i dont really know how, and in general, how can a regex be defined for infinite "sets"?

The string i want to validate is "ANYTHING|NUMBER_ONLY,ANYTHING|NUMBER_ONLY..."

for example: "hello|5,word|10" and "hello|5,word|10," are both valid.

note: I dont mind if the string ends with or without a comma ','.

2 Answers 2

1

Kleene star (*) lets you define "infinite sets" in regular expressions. Following pattern should do the trick:

[^,|]+\|\d+(,[^,|]+\|\d+)*,?
A----------B--------------C-

Part A matches the first element. Part B matches any following elements (notice the star). Part C is the optional comma at the end.

WARNING: Remember to escape backslashes in Java string.

Sign up to request clarification or add additional context in comments.

5 Comments

Wow, almost perfect. it seems that a string like: "aa|3,aa|4,hi|aa, aa|6" have passed the test :<
Oh, I see. Please check the new version. [^,|] matches anything except , and |.
And amazingly, it works! that's quite unbelievable, if ure about to open a regex course, im in! Thanks again!
Im really interested to know if i also understand it: correct me if im wrnog but the first part "[^,|]+\|" says match anything with | at the end (when u excluded ',' and '|' at the beginning) and when using + u expect it to be only this | to end the first part, and the second part "\d+(,[^,|]+\|\d+)*,?" means: "\d" any number + (same sequence from beginning)* when the * notation is the kleene star which lets us define "infinite sets" like u said, and as for the ",?" it means it can end either with or without a ',', have i understand it right?
Congrats! You missed only +, or the Kleene plus. A+ means "one or more As". Similarly, [^,|]+ means "one or more non-comma non-pipe characters" and \d+ means "one or more digits". The Kleene star is the "zero or more" version of the same thing.
1

I'd suggest splitting your string to array by | delimiter. And validate each part separately. Each part (except first one) should match following pattern \d+(,.*)?

UPDATED

Split by , and validate each part with .*|\d+

4 Comments

That doesn't quite make sense given the test input. Comma is the higher-level separator and | is the inner separator.
@MarkoTopolnik hm, really. Thanks, I didn't notice it.
Marko is right.. how do u separate between numbers to any other words?
Thank you I am trying at the moment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.