5

(This is a spinoff from a question I asked earlier.)

I am attempting to devise a RegEx for LibreOffice Writer that finds all abbreviations in my PhD thesis. Currently I have the following:

\b(?:[A-Z]){2,}

This almost does the job, as it identifies all words beginning with more than one capital letter. However, I have some abbreviations it does not catch, namely these:

CoE RoR RoC

Ideally I would like a RegEx that identifies all words with at least two capital letters, although they don't have to be at the beginning of the word. But I'm at a loss trying to create it. Can anyone point me in the right direction?

2 Answers 2

7

I interpreted the question a little differently from Jim K. Assuming that all abbreviations start with a capital letter and contain at least one more capital letter anywhere in the word, you don't have to add much to your existing regular expression:

\b(?:[A-Z][a-z]*){2,}

Capital letters are paired off with any number of lowercase letters, effectively eliminating the space between capital letters in a word. At least two of these pairs means that there are at least two capital letters.

If abbreviations must contain at least two capital letters but do not have to start with one, add an extra lowercase letter check in front of the capital letter check:

\b(?:[a-z]*[A-Z][a-z]*){2,}

Both of these were tested against Jim's test text. (thanks, Jim!)

1
  • Sorry for the late reply, but I wanted to test it on my actual thesis first. Happy to report that it worked flawlessly!
    – Øby
    Feb 26, 2017 at 12:55
2

This expression doesn't find all possibilities, but it should find the ones you're looking for:

\b(?:[A-Z]{2}[:alpha:]*)|(?:[A-Z][a-z][A-Z][:alpha:]*)

So for this text:

EULEX CFSP OJ CAbc cAB cAb cab Cab CabC CFSP OJ CFSP OJ EULEX EULEX EU EU
CoE RoR RoC

It finds all words except cAB cAb cab Cab CabC, which I presume should not be considered abbreviations.

Breakdown of the expression:

  • \b is the word boundary.
  • (?:[A-Z]{2}[:alpha:]*) accepts all words starting with two upper case letters.
    • [:alpha:]* matches the rest of the word by accepting any letters, whether upper or lower.
  • | "or" tries the next expression if the first doesn't match.
  • (?:[A-Z][a-z][A-Z][:alpha:]*) accepts all words starting with upper, lower, upper.
1
  • Another great suggestion. A shame I can't mark both as the correct solution. Which to prefer really depends on the situation. For meg, both RegExps yielded identical results, due to the nature of my text.
    – Øby
    Feb 26, 2017 at 12:56

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.