I am trying to create a regex for the following combination
Alphanumerictext1:1234:12:124:aplhnumericText2:www.stackoverflow.com
In the above format it should validate even if the text has alphanumeric(first field) or incremental values separated by :.
Below are few valid examples:
Alphanumerictext1
Alphanumerictext1:1234
Alphanumerictext1:1234:12
Alphanumerictext1:1234::124:aplhnumericText2
Alphanumerictext1::12
Alphanumerictext1:1234:12:124:aplhnumericText2:www.stackoverflow.com
For that wrote a partial regex like below ^([\w\s\.]+(:\d*){0,3})$
but unable fulfil for last 2 values that are separated by :aplhnumericText2
and www.stackoverflow.com
.
You may use this regex to validate all the examples provided:
^[\w\h.]+(?:(?::\d*){1,3}(?::\w*(?::\w+(?:\.\w+)*)?)?)?$
RegEx Details:
^
: Start[\w\h.]+
:(?:
: Start non-capture group #1
(?::\d*){1,3}
: Match :
followed by 0+ digits. Match 1 to 3 occurrences of this.(?:
: Start non-capture group #2
:\w*
: Match :
followed 0+ word characters(?:
: Start non-capture group #3
:\w+(?:\.\w+)*
: Match :
followed by a host name)?
: End optional non-capture group #3)?
: End optional non-capture group #2)?
: End optional non-capture group #1$
: End