Problem with a gap in the RegEx register with capture groups

I have a regular expression that I'm trying to match with the following data types: each token is separated by an unknown number of spaces.

Refresh : “Text” can be almost any character, so I had it .*originally. It is important to note that it may also contain spaces.

  • Text
  • Text 01
  • Text 01 of 03
  • Text 01 (from 03)
  • Text 01-03

I would like to write “Text”, “01” and “03” as separate groups, and all but “Text” are optional. The best I've been able to do so far:

\s*(.*)\s+(\d+)\s*(?:\s*\(?\s*(?:of|-)\s*(\d+)\s*\)?\s*)

# 3- # 5 . , , ? , 01 , .

\s*(.*)\s+(\d+)\s*(?:\s*\(?\s*(?:of|-)\s*(\d+)\s*\)?\s*)?

RegEx # 2- # 5, # 2 # 5.

, , .

- RegEx, , : http://regexr.com?2tb64. RegEx .

+3
3

, , , Javascript. :

var re = /^\s*(.+?)(?:\s+(\d+)(?:(?:\s+\(?of\s+|-)(\d+)\)?)?)?$/i;

Regexr, " ".

PHP ( !):

$re = '/ # Always write non-trivial regex in free-space mode!
    ^                  # Anchor to start of string.
    \s*                # optional leading whitspace is ok.
    (.+?)              # Text can be pretty much anything.
    (?:                # Group to allow applying ? quantifier
      \s+              # WS separates "Text" from first number.
      (\d+)            # First number.
      (?:              # Group to allow applying ? quantifier
        (?:            # Second number prefix alternatives
          \s+\(?of\s+  # Either " of 03" and " (of 03)",
        | -            # or just a dash  for "-03" case.
        )              # End second number prefix alternatives
        (\d+)          # Second number
        \)?            # Match ")" for " (of 03)" case.
      )?               # Second number is optional.
    )?                 # First numebr is optional.
    $                  # Anchor to start of string.
    /ix';
+6

, : regexr, .

\s*(\w*)\s+(?:\s*(\d+)\s*(?:\s*\(?\s*(?:of|-)\s*(\d+)\s*\)?)?)?
+1

:
http://regexr.com?2tb67

Regex :

(\w+?)\s+(\d*)[^\d]*(\d+)

, , , , , .

, , , , 01 . .

0

All Articles