Regex to match series and number from a string starting with predefined characters

Viewed 169

Is it possible to match last series and number from a string that looks like:

 CUI: RO39890982                                   BZ 347

What I want: BZ and 347 from two separate regular expressions

What I'm trying: CUI:[ ]*\s\S*\s(.*)

What I get: BZ 347

Any help is much appreciated! Thanks.

3 Answers

If you want two separate expressions:

Regex 1 using (\S+) to capture 1 or more non whitspace chars in group 1:

CUI:\s*\S+\s+(\S+)

Regex demo


Regex 2 Repeating a non capture group with a quantifier {2} with a nested capture group for the value

CUI:\s*\S+(?:\s+(\S+)){2}

Regex demo

Using the quantifier might be handy if you want to reuse it for more fields.

With your shown samples, please try following regex.

1st regex: To get BZ value: Online demo for following regex

\bCUI:\s+\S+\s+(\S+)

Explanation: Adding detailed explanation for above regex:

\bCUI:      ##Matching a word boundary with string CUI: here.
\s+\S+\s+   ##Matching 1 or more spaces followed by 1 or more non-spaces, followed by 1 or more spaces here.
(\S+)       ##Creating 1st capturing group which has 1 or more non-spaces here, which will catch BZ value here.


2nd regex: To get 347 value(or similar pattern values) with shown samples.

\bCUI:\s+\S+\s+\S+\s+(\S+)

Explanation: Adding detailed explanation for above.

\bCUI:     ##Matching a word boundary with string CUI: here.
\s+\S+\s+  ##Matching 1 or more spaces followed by 1 or more non-spaces, followed by 1 or more spaces here.
\S+\s+     ##matching 1 or non-spaces followed by 1 or more spaces here.
(\S+)      ##Creating 1st capturing group which has 1 or more non-spaces here, which will catch 347 value here.

Online demo for above regex

Get 347 with

CUI: *\S+ +[A-Z]+ +(\d+)

Get BZ with

CUI: *\S+ +([A-Z]+)

Regex proof #1, regex proof #2.

EXPLANATION

--------------------------------------------------------------------------------
  CUI:                     'CUI:'
--------------------------------------------------------------------------------
   *                       ' ' (0 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  \S+                      non-whitespace (all but \n, \r, \t, \f,
                           and " ") (1 or more times (matching the
                           most amount possible))
--------------------------------------------------------------------------------
   +                       ' ' (1 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  [A-Z]+                   any character of: 'A' to 'Z' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
   +                       ' ' (1 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
Related