0

I have a comma separated string which I want to validate using a regex. What I have written is gives me a match if there a part wrong later in the string. I want to discard it completely if any part is wrong.

My regex : ^(?:[\w\.]+,{1}(?:STR|INT|REAL){1},{1}(\s*|$))+

Positive Case : Component,STR,YoungGenUse,STR,YoungGenMax,STR,OldGenUse,INT,OldGenMax,INT,PermGenUse,INT,PermGenMax,INT,MajCollCnt,INT,MinCollDur,REAL,MinCollCnt,INT,

Negative Case : Component,STR,YoungGenUse,STR,YoungGenMax,TEST,OldGenUse,INT,OldGenMax,INT,PermGenUse,INT,PermGenMax,INT,MajCollCnt,INT,MinCollDur,REAL,MinCollCnt,INT,

For the second case, my regex gives a match for the bold portion eventhough, later there is an incorrect part (TEST). How can I modify my regex to discard the entire string?

The fourth bird
  • 96,715
  • 14
  • 35
  • 52
amat_coder
  • 21
  • 4

2 Answers2

2

The pattern that you tried would not match TEST in YoungGenMax,TEST because the alternatives STR|INT|REAL do not match it.

It would show until the last successful match in the repetition which would be Component,STR,YoungGenUse,STR,


You have to add the anchor at the end, outside of the repetition of the group, to indicate that the whole pattern should be followed by asserting the end of the string.

There are no spaces or dots in your string, so you might leave out \s* and use \w+ without the dot in the character class. Note that \s could also possibly match a newline.

^(?:\w+,(?:STR|INT|REAL),)+$

Regex demo

If you want to keep matching optional whitespace chars and the dot:

^(?:[\w.]+,(?:STR|INT|REAL),\s*)+$

Regex demo

Note that by repeating the group with the comma at the end, the string should always end with a comma. You can omit {1} from the pattern as it is superfluous.

The fourth bird
  • 96,715
  • 14
  • 35
  • 52
  • Thanks a lot, your explanation has really helped me! Could you also point me to some to good resources to learn regex? I'm have just started learning. Not very good at it. – amat_coder Jul 10 '20 at 15:48
  • @BhavyaBansal You are welcome! You could checkout for example https://www.regular-expressions.info/ or https://www.rexegg.com/ or the Stack Overflow FAQ https://stackoverflow.com/a/22944075/5424988 – The fourth bird Jul 10 '20 at 15:51
0

your regex must keep matching until end of the string, so you must use $ to indicate end of the line: ^(?:[\w.]+,{1}(?:STR|INT|REAL){1},{1}(\s*|$))+$

Regex Demo

mjrezaee
  • 1,040
  • 2
  • 9