regular expression help

CalmSoul
CalmSoul used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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

Author

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...
Terry WoodsIT Guru
Most Valuable Expert 2011

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

Open in new window

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial