0

I have a specific request to all you regex wizards :) I want to create a simple function that deletes or cleans out several parts of a text file based on predefined keywords (such as single capital letter, some changing phrases that start end with same words etc).

I want to use the function to clean out references based on pre defined words/sentences. for example this is how the original (initial) reference will look:

6. F. NordioPD, A. ZanobbeiC, I. & J. D. SchwratzPI. 2015. Changing patterns of the temperature-mortality association by time and location in the US, and implications for climate change 81, 80-86. Environment International (3 Citations, IF= 5.559 ; Q1 Environmental Sciences , 8 of 221)

what i want is to clear out:

1) all the added words to a name such as NordioPD note the PD part. the words that we use are 'PD' ,'C' or 'PI' based on the type of contributor and are attached in uppercase to the author last name.

2) in addition there is always a line at the end of the reference that looks like this (3 Citations, IF= 5.559 ; Q1 Environmental Sciences , 8 of 221)

the line always starts with (X citations) where X can be any number and ends with the ) .

using the above example the reference i want to end up with will look something like this:

6. F. Nordio, A. Zanobbei, I. & J. D. Schwratz. 2015. Changing patterns of the temperature-mortality association by time and location in the US, and implications for climate change 81, 80-86. Environment International.

Any regex wizard out there know how to deal with this?

  • This is going to be a lot easier if you show an example in context, and describe how to distinguish a thing-to-delete from all the things-to-keep – phils Jun 23 '16 at 2:46
  • I need more information. Your example text above seems like the way you want it to come out, but how is it initially, and what are the rules for adding those strings? How do you determine the "type of contributor"? Does the "(3 Citations..." always appear exactly like that? – jtgd Jun 23 '16 at 6:09
  • I just edited the above question to add a example in context as requested. thx – zeltak Jun 23 '16 at 7:20
  • So we identify a line that needs changing by the presence of the pattern ([0-9]+ citations, removing that section and any preceding instances of the upper-case word suffixes PD, C, or PI if they are preceded by a lower-case letter?? Is it possible that there would be additional parentheses inside the (X citations, ....) section? Is that all in a single line? Or in a single blank-line-separated paragraph? Could a line break occur between the number and the word "citations"? Are you adding a full stop in place of the citations text? You can't do this without establish the rules. – phils Jun 24 '16 at 0:33
  • 1
    thx. yes your first assumption is dead on (he presence of the pattern ([0-9]+ citations, removing that section and any preceding instances of the upper-case word suffixes PD, C, or PI if they are preceded by a lower-case letter). the line with the (X citations, ....) cannot have further parens inside it. its always at the end of the reference (a reference is a paragraph) and is separated by a dot and then a single whitespace from the end of the paragraph (its the last line in the reference block). the reference example in the question is exactly how it looks all the time. thx – zeltak Jun 24 '16 at 16:40
2

This works for the input you supplied. Select the text and call the function (I'm not sure what your preferred workflow would be so I used active region) If you find input it fails for let me know and I can update it.

  (defun cleanup-refs ()
"Delete portions of reference section within region."
(interactive)
(let ((start (region-beginning)))
  (while (re-search-forward " ?([0-9]+ Citations[\n -~]*?)" (region-end) t) (replace-match ""))
  (goto-char start)
  (while (re-search-forward "\\(C\\|PD\\|PI\\)\\>" (region-end) t) (replace-match ""))
  (goto-char (region-end))
))
  • 1
    This looks more like a comment instead of an answer? Maybe you should convert it to a comment and delete this answer until you have a defnitive solution? – lawlist Jun 23 '16 at 0:57
  • thx jtgd. i region marked ~ 20 references to test the above function yet it seemed to wipe all but 2 out of 20 references. i further clarified the structure in the above comment if that helps.Z – zeltak Jun 24 '16 at 16:46
  • I imagined that you would select one reference at a time. If selecting a bunch of them then I need to know how to delimit a reference. Is there a blank line between? Are there never any blank lines within a reference? In this case I could go by paragraphs. Is there no other occurrences of parens except the citation count? If so then I could process as one big chunk (basically change that when to a while) – jtgd Jun 25 '16 at 0:18
  • thx yes there are no empty lines in a reference (you can say each reference is a contiguous paragraph). also each reference is divided by a single empty (blank) line. also yes there should be no other parens other than the citation counter. thx again! Z – zeltak Jun 25 '16 at 4:33
  • I edited my answer to make a few minor changes. I made the when to a while and slightly changed a regexp. I did not make it conscious of paragraphs so blank lines should not matter. See if that works for you. – jtgd Jun 25 '16 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.