Avatar of CalmSoul
CalmSoul
Flag for United States of America asked on

regular expression help

What will be the regular expression to read following text file - I would like to make an array

Any text under [element] in one array
Any text under [Docs] in one array
Any text under [Indices.1] in one array
Any text under [Indices.2] in one array


[element]
Type=OU Document with type

[Docs]
1=\ft\ou\ds\comb\5623589556536.TIF
2=\ft\ou\ds\comb\5623589556586.TIF

[Indices.1]
account_number=9999999
first_name=test user
address=0
sub_class=OUT
linux_date=20000630
bentch_date=  /  /  
mod_acct_num=



[Indices.2]
account_number=9999999
first_name=test user
address=0
sub_class=OUT
linux_date=20000630
bentch_date=  /  /  
mod_acct_num=

Open in new window

Regular Expressions

Avatar of undefined
Last Comment
Dan Craciun

8/22/2022 - Mon
Dan Craciun

While it can be done with regular expressions, it's not the tool for this situation. You'll get 4 strings that you'll need to parse anyway.

You need to write a parser. How to do that depends on your platform.

LE: in no particular language, the logic for the parser would be something like this:
function create_array(file, begining)
  array=[]
  find begining in file
  line=readline
  while line not empty
    array.push(line)
    line=readline
  end while
  return array
end function

open file
element_array=create_array(file, "[element]")
docs_array=create_array(file, "[Docs]")
indices1_array=create_array(file, "[Indices.1]")
indices2_array=create_array(file, "[Indices.2]")
close file

Open in new window


HTH,
Dan
CalmSoul

ASKER
Dan

If you can provide me regular expression to get the values before and after "="... I can write the parser... I just need to get the values out...
Terry Woods

Try this for a single line with an = character:
preg_match("/([^=]*)=(.*)/", $line, $matches);

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Dan Craciun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.