Home > Blockchain >  Regular expression to match flattened attributes
Regular expression to match flattened attributes

Time:02-03

I have specific flattened property names that I need to validate. Basically, what's valid is alpha-numeric string that can be followed by an array index ([4] for example), but doesn't have to be. If it's followed by a next word, then it has to be joined by a dot.

To illustrate, some of the examples that would be valid are:

parent[0].child
parent.child
parent.child1.list[0].child2
parent[500].child
parent0.child
par50ent.child
parent[0].child1[20].child[0]

And some invalid examples are

parent..child
parent[].child
parent[1a].child
parent[a].child
.parent
parent.

I don't know much about regular expressions, so what I came up with is this following line:

(([a-zA-Z0-9]{1,})(\[[0-9]{1,}\]){0,1}){1}((\.){1}([a-zA-Z0-9]{1,})(\[[0-9]{1,}\]){0,1}){0,}

While this does work, it's just too verbose. There has to be a nicer, shorter way to write this.

CodePudding user response:

You may use this regex to match only valid cases:

^\w (?:\[\d ])?(?:\.\w (?:\[\d ])?)*$

RegEx Demo

RegEx Details:

  • ^: Start
  • \w : Match 1 word characters
  • (?:\[\d ])?: Match optional [<digits>] part
  • (?:: Start a non-capture group
    • \.: Match a dot
    • \w (?:\[\d ])?: Match a word followed by optional [<digits>]
  • )*: End non-capture group. Repeat this group 0 or more times
  • $: End

You may use this regex if you want to allow parent[0][1].child as well:

^\w (?:\[\d ])*(?:\.\w (?:\[\d ])*)*$

CodePudding user response:

Assuming that letters are English ones (A..Z or a..z) while digits are 0..9, you can try

^[A-Za-z] [A-Za-z0-9]*(?:\[[0-9] \])*(?>\.[A-Za-z] [A-Za-z0-9]*(?:\[[0-9] \])*)*$

pattern:

^               - anchor, string start 
[A-Za-z]        - starts from letter
[A-Za-z0-9] *   - can contain zero or more letters or digits
(?:\[[0-9] \])* - can end by zero or more [digits] indexers

(?>\. ...)*     - followed by zero or mo costructions each of them
                  starts from dot . and has syntax equals to the one above 
$               - anchor, string end 

Fiddle

Edit: please, note atomic group (?> ...) to prevent catastrophic bactracking (see The fourth bird's comment below)

  •  Tags:  
  • Related