Regex to find and remove Abbreviations which are either ABC or A.B.C

Viewed 316

In my data, there are lot of garbage things like IIT-JEE, AIPMT, C.B.S.E.. So I came to a regex to remove these by using (?<!\w)([A-Za-z])\. . It is removing C.B.S.E. BUT it is also removing x.y which is part of the useful equation.

So Basically I want to remove Abbreviations of length more than 2 so that lower case x.y is preserved but Upper Case X.Y.Z. is removed. Also I want to remove any words which are Upper Case and are of length>=3. Such as abc is preserved but IIT is removed.

What can I do to modify my existing regex or how can I make a new one to fulfil these tasks?

2 Answers

So Basically I want to remove Abbreviations of length more than 2 so that lower case x.y is preserved but Upper Case X.Y.Z. is removed. Also I want to remove any words which are Upper Case and are of length>=3. Such as abc is preserved but IIT is removed.

You may try this regex:

\b(?:[A-Z]{3,}\b|(?:[A-Za-z]\.){2,})\s*

RegEx Demo

RegEx Details:

  • \b: Word boundary
  • (?:: Start a non capture group
    • [A-Z]{3,}\b: Match a word of 3+ uppercase letters
    • |: OR
    • (?:[A-Za-z]\.){2,}: Match an abbreviation of letter followed by dot of min length=2
  • ): non capture group
  • \s*: Match 0 or more whitespaces

You could use

(?<!\S)(?:(?:[A-Za-z]\.){3,}|[A-Z]{3,})(?!\S)

Explanation

  • (?<!\S) A whitespace boundary to the left
  • (?: Non capture group
    • (?:[A-Za-z]\.){3,} Repeat 3 or more times A-Za-z followed by a dot
    • | Or
    • [A-Z]{3,} Match 3 uppercase chars
  • ) Close non capture group
  • (?!\S) A whitespace boundary to the right

Regex demo

Related