RegEx and corresponding codes from right to left

Digressing a bit with RegEx, I have 4 codes per line

code4: CODE3: Codex2: code1

every code is optional except CODE1

So I could have ab: bc: de: fg

or

Bc :: fg

of

ab: fg

In every case of the above CODE1 = fg dnd for my dear life, I cannot develop a Regex

It would be easy to do as standard parsing, but no coincidence because of the objects buisness needs to be done via regex :-( and back via vb.net Regex.matche, groups ("Code1") fg (I hope that makes sense)

Thanks in advance for your help

Ending out a RegEx bit that does the job, bit a mess but it works

(^(?<code1>[\w]*)$)|(^(?<code2>[\w]*):(?<code1>[\w]*)$)|(^(?<code3>[\w]*):(?<code2>[\w]*):(?<code1>[\w]*)$)|(^(?<code4>[\w]*):(?<code3>[\w]*):(?<code2>[\w]*):(?<code1>[\w]*)$)

      

Ta all

0


source to share


3 answers


There is no need to use a regular expression here.

I don't know what language you are using, but split the string by ':' and you have an array of codes.

If you really want to check if the string is correct for this then



/(\w*:){0,3}\w+/

      

matches your description and several examples you have given.

+4


source


I'm not sure why you need to match codes from right to left. Just use a regex to highlight the line:

/(.*):(.*):(.*):(.+)/

      



and then you have CODE1 at $ 4, CODE2 at $ 3, CODE3 at $ 2, CODE4 at $ 1.

0


source


(CODE1)?:(CODE2)?:(CODE3)?:CODE4

will work - if the leader: doesn't matter. Otherwise, if you can't have leading colons, list:

(CODE1:(CODE2)?:(CODE3)?:|CODE2:(CODE3)?:|CODE3)?CODE4

There is nothing special about the right side being required and the left-most side not.

0


source







All Articles