amat_coder
amat_coder

Reputation: 31

Regex - Discard the entire string if any part of the string doesn't match the pattern

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?

Upvotes: 1

Views: 169

Answers (2)

The fourth bird
The fourth bird

Reputation: 163427

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.

Upvotes: 2

Javad Rezaei
Javad Rezaei

Reputation: 1110

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

Upvotes: 0

Related Questions