Question: I'm here trying to simplify a regex.

What i tried was doing a repetition for the same expression, but when I tried to simplify it using ()\*, it does not worked as it does not detect the pattern I want.

These are my regex:

```
(([\(]\w]{1,3}[\)])\s([\d]{1,3}[\?])([\(][\w]{1,3}[\)])\s[\d]{1,3}[\?]([\(][\w]{1,3}[\)])\s([\d]{1,3}[\?])([\(][\w]{1,3}[\)])\s[\d]{1,3}[\?]([\(][\w]{1,3}[\)])\s[0-9]{1,3}[\?]([\(][\w]{1,3}[\)])\s[\d]{1,3}[\?])

```

Whole pattern:

```
3A 1?(1) 2?(2) 3?(a) 4?(4) 5?(a) 6?(ii) 7?
4 6?(1) 7?(2) 8?(a) 9?(4) 10?(a) 11?(ii) 12?  

```

These are the patterns it will detect:

```
1?(1) 2?(2) 3?(a) 4?(4) 5?(a) 6?(ii) 7?

```

The regex will detect **ONLY** the **first LINE** without the **3A**. How can I do these? 
The regex already be at it's simplest that is `\d+\?(?:\([\da-z]+\))?` , and now how can I put it in a way to detect **first line** only? Thanks guyss.

Answer: 
Maybe, here we can simplify it in another way, if it would be possible. For example, we might have three patterns, which we can capture it using three capturing groups: start, end and middle groups, possibly similar to:

```
(?:^\w+\s)|(\d\?\(\w+\)\s)|(?:\d+\?$)

```

[![enter image description here](https://i.stack.imgur.com/YuLY3.png)](https://i.stack.imgur.com/YuLY3.png)

### RegEx

If this wasn't your desired expression, you can modify/change your expressions in [regex101.com](https://regex101.com/r/IfUwEV/1), and add or reduce the boundaries that you wish.

### RegEx Circuit

You can also visualize your expressions in [jex.im](https://jex.im/regulex/#!flags=&re=%5E(a%7Cb)*%3F%24):

[![enter image description here](https://i.stack.imgur.com/P8vpv.png)](https://i.stack.imgur.com/P8vpv.png)

### JavaScript Demo for Capturing Groups

```js
const regex = /(?:^\w+\s)|(\d\?\(\w+\)\s)|(?:\d+\?$)/gm;
const str = `3A 4?(1) 5?(2) 6?(a) 7?(4) 8?(a) 9?(ii) 10?`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }

    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}
```
Try this pattern: `\d+\?(?:\([\da-z]+\))?`

Explanation:

`\d+` - match one or more digits

`\?` - match `?` literally

`(?:...)` - non-capturing group

`\(` - match `(` literally

`[\da-z]+` - match one or more of either digit or lowercase letter

`\)` - match `)` literally

`?` - match at most one time preceeding pattern, which is `\([\da-z]+\)`

[Demo](https://regex101.com/r/jWD9aN/1)

Usage in code:

```
Sub Main()
    Dim matches = Regex.Matches("3A 4?(1) 5?(2) 6?(a) 7?(4) 8?(a) 9?(ii) 10?", "\d+\?(?:\([\da-z]+\))?")
    For Each match As Match In matches
        Console.WriteLine(match.Value)
    Next
    Console.ReadKey()
End Sub

```

**UPDATE**:

Try updated pattern: `^(?:\d+(?:\?(?:\([\da-z]+\))?|[A-Z]+) ?)+$`

[Demo](https://regex101.com/r/bcXaTR/1)