Regular Expression to validate a string containing '=' and '~' as end character

kam kam :

In Java, I have to validate a string which contains "~" and '=' at the end using RegEx.

For example:

LOCKER=2004-02-23-23.28.22.377655~UCC=0103207031~URY=31/12/9999~URF=23/02/2004~URT=SEREST ISSY LES MO      ~URFC=XX~URFNUMCB=XXXXXXXXXXX~CEB=XXXXX~CEBC=XXXXX~URFN=0001

this String format is KEY1=VALUE~KEY2=VALUE~KEYN=VALUE uppercase

'~' is as separator

Currently, i am using some regular expresion but all of them false can anyone help me please ? thank you for advanced

Andreas :

The following regex should do:

^(?!~)(?:(?:^|~)[^=~]+=[^=~]*)+$

Explanation

^            Match beginning-of-input, i.e. matching must start at beginning
(?!~)        Input cannot start with `~`
(?:          Repeat 1 or more times:
  (?:^|~)      Match beginning of input or match '~', i.e. match nothing on
               first repetition, and match `~` on each subsequent repetition
  [^=~]+       Match KEY
  =            Match '='
  [^=~]*       Match VALUE (may be blank)
)+
$           Match end-of-input, i.e. matching must cover all input

Change the characters classes for KEY and VALUE as needed if they have further restrictions, e.g. use [A-Z][A-Z0-9]* instead of [^=~]+ if the KEY has to be an uppercase-only identifier.

If using Java's matches() with the regex, the first ^ and the ending $ is redundant. The second ^ is still required.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=237478&siteId=1