Dave Long
Dave Long

Reputation: 9759

Regular Expression Within a Regular Expression

I have a a CSV file that a client gave me that I need to turn into a bunch of if statements for a program I am working on. The data looks like the following:

Alfred E. Burr     A.E Burr     A.E Bu     Burr
A.I Prince     Prince R.V.T.S     Prince Tech

And I need to turn that into:

if(school IS 'Alfred E. Burr' OR school IS 'A.E Burr' OR school IS 'A.E Bu' OR schools IS 'Burr')
else if(school IS 'A.I Prince' OR school IS 'Prince R.V.T.S' OR school IS 'Prince Tech')

I already have the code to go after the if statement written. I would hand code it, but there are ~150 school in the list.

So far I have built this regular expression which matches a whole line, but I am not sure it I can use the sub expression that I created in it to match each school within the line:

^(([A-Za-z0-9\.\ \&\']+)\t?)+$

So working with that base how would I code the regular expression to match each line and then within those lines each school and is it even possible?

Upvotes: 0

Views: 150

Answers (3)

Sam Choukri
Sam Choukri

Reputation: 1904

Well, I wouldn't do it in Eclipse, if you can avoid it. If you have access to a command line, here's a perl-one liner that you can use:

perl -lanF"\t" -e $'print "else if (" . join(" OR ", map {"school IS \'$_\'"} @F) . ")"' input_file

For simplicity, this command outputs "else if" for all lines, including the first one. You will have to change the first line manually.

By the way, you stated your input data is a CSV file (comma-separated values) but it appears it is actually a tab-delimited file. My solution only works with a tab-delimited file as input.

Upvotes: 2

josh.trow
josh.trow

Reputation: 4901

Not really sure what you are trying to do...are you looking for code to build the giant if block for you? If so, here's some (fake) code, based on Java:

Pattern p = "([a-z-A-Z\s\.]+)"; // School names - adjust the pattern as neeeded
String ifBlock;
for (i = 0; i < data.lineCount; i++)                  // Lines in a file
  String schools = '';
  while (p.find())                                    // School name in a line
    schools += "school IS " + match + " OR ";
  end
  if (school.length > 0)
    school = school.substring(0, school.length - 3);  // trim the trailing 'OR'
  ifBlock += "else if(" + school + ") { \n <<EXECUTION CODE>> \n }"; // add to block
end
if (ifBlock.length > 0)
  ifBlock = ifBlock.substring(5);                     //Remove leading 'else'

Upvotes: 0

stema
stema

Reputation: 93026

I am not sure what you want to do with your regex.

I would do this, as this is a one time job, with search replace in three steps. I don't know what eclipse is able to do but is should be able to do this also.

  1. Step: Insert at every beginning of a row "else if(school IS '". For the first row, do it by hand.

  2. Step: Replace " " (5 Spaces, as in your example above) with "' OR school IS '"

  3. Step: Insert at every row end "')"

Upvotes: 0

Related Questions