简体   繁体   中英

Regex to select the words those match brackets

I need to extract the full forms using regex using javascript

I have tried with

(\w+\s*[^a-z^A-Z]*){3}\s*\([A-Z]*\)

but the extraction fails when there are some full forms like these

Most common mis'take (MCM) (only bold is selected)

Below is the text for testing:

The task of automatically extracting acronymdefinition pairs from biomedical literature has

Most common mis'take (MCM) been studied, almost exclusively for English, over the past few decades using technologies from Natural Language Processing (NLP). This section 167 presents a few approaches and techniques that were applied to the acronym identification task. Taghva and Gilbreth (1999) present the Acronyms 7'- $ **** Finding Program (AFP)

, based on pattern matching. Their program seeks for acronym candidates which appear as upper case words. They calculate a heuristic score for each competing definition by classifying words into: (1) stop words (”the”, ”of”, ”and”), (2) hyphenated words (3) normal words (words that don't fall into any of the above categories) and (4) the acronyms themselves (since an acronym can sometimes be a part of the definition). The AFP utilizes the Longest Common Subsequence (LCS) algorithm (Hunt and Szymanski, 1977) to find all possible alignments of the acronym to the text, followed by simple scoring rules which are based on matches. The performance reported from their experiment are: recall of 86% at precision of 98%

Instead of repeating the group 3 times, you could use 3 capturing groups with a backreference to those groups matching the first letter of the word.

\b(\w)[\w']*[^a-zA-Z()]* (\w)[\w']*[^a-zA-Z()]* (\w)[\w']*[^a-zA-Z()]* \(\1\2\3\)
  • \b Word boundary
  • (\w) Match a single word char in group 1
  • [\w']* Match 0+ times a word char or '
  • [^a-zA-Z()]* Match 0+ times any char except the listed, then match a space
  • (\w)[\w'] [^a-zA-Z()] Same as above with group 2
  • (\w)[\w'] [^a-zA-Z()] Same as above with group 3
  • (\1\2\3) Between parenthesis, use the 3 backreferences to the capturing groups

Regex demo


You could also update your pattern by adding the ' to the character class and repeat that 0+ times [\w']*

You can extend the character class with characters you would allow to match.

\b(?:\w[\w']* [^a-zA-Z]*){3} ?\([A-Z]{3}\)
  • \b Word boundary
  • (?: Non capture group
    • \w[\w']* Match a word char and 0+ times any char except a word char or ', then match a space
    • [^a-zA-Z]* Match 0+ times any char except a-zA-Z
  • ){3}? Repeat 3 times and match optional space
  • \([AZ]{3}\) Match 3 occurrences of AZ between parenthesis

Regex demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM