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

LVL 5
CalmSoulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
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
0
CalmSoulAuthor Commented:
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...
0
Terry WoodsIT GuruCommented:
Try this for a single line with an = character:
preg_match("/([^=]*)=(.*)/", $line, $matches);

Open in new window

0
Dan CraciunIT ConsultantCommented:
If you need the values before and after = you need an associative array. Below is the modified code for that:

function create_array(file, begining)
  array=[]
  find begining in file
  line=readline
  while line not empty
    terms=explode("=", line, 2)
    array[terms[0]]=terms[1]
    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


This really is not the job for regular expressions.
a. once you have a line you can simply explode on "=" to get your terms
b. 6 months from now if you need to review your code you'll understand at a glance what this piece does, while using regular expressions will be hard to decipher.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.