Skip to content
Advertisement

regex convert to object

I’m trying to get the value as regex as follow:

JavaScript

But I can get the only single value such as “fixed-address” and “host2name”. In “domain-name-servers” I did with “,” in regex. But I think it isn’t the right way because the values are not same count. Could you help me to get the value of “domain-name-servers” and “domain-search” with right regex?

ref: Parsing dhcpd.conf with textX

Advertisement

Answer

The easiest way is to use textX’s repetition modifiers for matching a sequence of comma-separated values. Basically, whenever you match zero-or-more or one-or-more etc. you can add modifier in the square brackets. The most frequently used modifier is Separator modifier which basically is a match that is used between each two elements.

The other side bonuses instead of trying to match everything with a single regex are:

  • simplicity (easier to maintain)
  • you get a nice Python list of elements so you don’t need to process the matched string further.

The working grammar would be (notice the use of +[','] which means one-or-more with a comma as a separator):

JavaScript
Advertisement