M
M
MdaUZH2016-02-03 18:55:38
PHP
MdaUZH, 2016-02-03 18:55:38

How to correctly compose a regular expression for cutting on ()?

Hello everyone..
There is a string:
(name)(type)(percent)(who)
name - any string, spaces, -, and other characters..
desc - any number ([\d])
percent - any string, any characters
who - any string and characters
But (who) in the string is not always.
I got something like this:

/(\([\2 ]*\))[\s]*(\([\S ]*\))[\s]*(\([\S ]*\))[\s]*(\([\S ]*\))?/i

but it cuts in brackets somehow not right ...
Tell me how to get what is in brackets correctly?
each parenthesis is a separate element of the array
, it is also necessary to cut such a line - (cgfddfv fl)(3)(yo 15%)( vk.com/sda)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cat Anton, 2016-02-03
@MdaUZH

\((?<name>[^)]+)\)\((?<type>\d+)\)\((?<percent>[^)]+)\)(?:\((?<who>[^)]+)\))?

https://regex101.com/r/mS1vE2/3

M
Muhammad, 2016-02-03
@muhammad_97

\(([^\)]*)\)\(([\d]*)\)\(([^\)]*)\)\(([^\)]*)\)
https://regex101.com/r/rN8tG2/1

M
Max, 2016-02-03
@AloneCoder

https://regex101.com/r/aI3fY3/1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question