Regex to match string which has words followed by whitespace then digits dot or hyphen and words followed by space and then (some info)

Viewed 282

I have a string which has the following format: name category (more info)

eg: Foo Bar-8.io 5.61.0-rc-1 (data)

I need a regex which basically filters out strings conforming to the above format.

name can be alpha numeric with spaces, - and .

category can start with digit followed by word including dot or hyphen

data can be anything .* enclosed in ()

I have tried this: ^[\w\s]+.*\s.*\(.*\)$ but doesnot seems to cover the above pattern.

1 Answers

Use

^(.*)\s+(\S+)\s+\((.*)\)$

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    \S+                      non-whitespace (all but \n, \r, \t, \f,
                             and " ") (1 or more times (matching the
                             most amount possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \3
--------------------------------------------------------------------------------
  \)                       ')'
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Related