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}`);
    });
}
```
This pattern `([\(][\w]{1,3}[\)])\s[\d]{1,3}[\?]` does not match the leading `4?`

You could simplify the expression by remove the square brackets as you don't need them and add an optional part to match 1-3 digits followed by a question mark in front:

```
(?:\d{1,3}\?)?(\(\w{1,3}\))\s\d{1,3}\?

```

[Regex demo](https://regex101.com/r/g6yogC/1)

If you don't want separate matches but 1 single match and the digits question mark part can also occur without the following part between the parenthesis , you could use a repeating group with an optional part for the parenthesis:

```
\d+\?(?:\(\w{1,3}\))?(?: \d+\?(?:\(\w{1,3}\))?)+

```

**Explanation**

* `\d+\?` Match 1+ digits and `?`
* `(?:\(\w{1,3}\))?` Optional group to match the parenthesis part
* `(?:` Non capturing group
	+ `\d+\?(?:\(\w{1,3}\))?` Match 1+ digits and `?` followed by an optional part for the parenthesis
* `)+` Close non capturing group and repeat 1+ times

[Regex demo](https://regex101.com/r/g6yogC/2)

**Note**

In the first pattern of your example there is an opening bracket missing 

```
(([\(][\w]{1,3}
      ^ 

```

In the second expression there is a closing parenthesis `)` to much at the end of the pattern.