#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0

    Parsing config file to collect data


    I am wanting to parse a config file and collect data for a csv output file.

    The config file I'm working with happens to be an AdTran phone system. But it follow the semi-standard config file format of various network devices (e.g. Cisco routers) so I imagine this concept could be used in a variety of situations.

    Here is a sample portion of the config file (note: I randomly removed non-relevant lines of the config to shorten it up.)
    Code:
    !
    !
    !
    voice user 0451
      caller-id-override external-number 8305962366
      did "2366"
      no special-ring-cadences
    !
    !
    !
    voice user 101
      connect sip
      cos "executive_users"
      last-name "Membership Coordinator"
      password "3101"
      did "2350"
    !
    !
    !
    voice user 102
      connect sip
      cos "executive_users"
      last-name "Golf Shop"
      voicemail password "1234"
      voicemail notify schedule Sunday 12:00 am 
    !
    !
    !
    voice user 103
      connect sip
      cos "normal_users"
      did "2351"
      password "1234"
    !
    !
    !
    The source file will always be properly formatted, so I don't need to account for irregularities.

    As you can see, each voice user (extension) may or may not have a last-name and did associated with it. But if it does have one I want it included in the output file.

    Here is my current expression:
    Code:
    !\r\nvoice user (\d{3,4})[^!]*(?:last-name ("[^"]*"))?[^!]*(?:did ("\d{4}"))?[^!]*?\r\n!
    And my collection string is \1,\2\,3

    Sample output is
    Code:
    0451,,
    101,,
    102,,
    103,,
    If I remove the ? at the end of
    Code:
    (?:last-name ("[^\r\n]*"))?
    
    and
    
    (?:did ("\d{4}"))?[^!]*?
    It works as desired except that it only matches extensions that have both a last-name and did. (Which is the expected behavior since removing the ? made them no longer optional.)

    What I can't figure out is why the last-name and did are not collected in the back references when they exist for a particular extension. (Using the original expression I posted above, with the "optional" last-name and did.)

    I'm no regex expert, but can usually get things figured out. But this time I can't see where I'm going wrong. Any ideas?

    Thanks in advance.

    EDIT: P.S. I realize this can be done without much trouble with arrays and bit of searching, but I do like to keep in practice.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    225
    Rep Power
    41
    What do you want the output to look like?
    If your input is:

    Code:
    !
    !
    !
    voice user 0451
      caller-id-override external-number 8305962366
      did "2366"
      no special-ring-cadences
    !
    !
    !
    voice user 101
      connect sip
      cos "executive_users"
      last-name "Membership Coordinator"
      password "3101"
      did "2350"
    !
    !
    !
    voice user 102
      connect sip
      cos "executive_users"
      last-name "Golf Shop"
      voicemail password "1234"
      voicemail notify schedule Sunday 12:00 am 
    !
    !
    !
    voice user 103
      connect sip
      cos "normal_users"
      did "2351"
      password "1234"
    !
    !
    !
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0
    with the input above:
    Code:
    0451,,"2366"
    101,"Membership Coordinator","2350"
    102,"Golf Shop",
    103,,"2351"
    I started to explain what I thought was going on, but realized I was just re-stating what I said in my original post. Please let me know if any further clarification would be helpful.

    As an fyi: Just for testing I changed the non-capturing groups to standard ones. And changed my collection string to \1,\2,\3,\4,\5,\6,\7,\8,\9 and got the following output. (So I don't think I'm just counting back traces incorrectly.)
    Code:
    0451,,,,,,,,
    101,,,,,,,,
    102,,,,,,,,
    103,,,,,,,,
    EDIT: I didn't have this case in my sample input, but it also should match for a voice user that has a not("last-name" or "did") This case would yield a desired output of something like 104,, It seems as though it is handled correctly i.e. handled the same as voice user with last-name XOR did.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0
    I've done more testing/experimenting with groups nested inside of non-referencing optional groups.

    E.G.
    Source:
    voice user 101
    Regexp:
    Code:
    (?:voice (user))? 101
    With a collection string of \1 yields
    user
    Which is what I would expect to happen. It just furthers my bafflement why it is matching but not collecting the last-name and did in the original exp I posted.

IMN logo majestic logo threadwatch logo seochat tools logo