开发者

Regular Expression Within a Regular Expression

开发者 https://www.devze.com 2023-02-27 01:08 出处:网络
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:

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?


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.


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 "')"


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'
0

精彩评论

暂无评论...
验证码 换一张
取 消