Capture digit in parentheses

1

I have several types of texts, for example: $ticker="08.070.838/0001-63.offset(1)" .

I want to capture the text and the digit, if any, that is enclosed in parentheses to make it the "08.070.838/0001-63.offset+1" form.

I am trying to use the% regex '(\S*)\((\d)\)' used in php by preg_match as follows:

if(preg_match('(\S*)\((\d)\)', $ticker, $match)) $ticker=$match[0]."+".$match[1];;

However you are returning the error message:

  

Warning: preg_match (): Unknown modifier '\'.

Would anyone know what's wrong with any of these \ ?

    
asked by anonymous 08.11.2017 / 14:54

1 answer

1
  

I want to capture the text and the digit (if any) that is enclosed in parentheses

Try this regex: (".*?)\((\d*?)\)

  

[...] to transform it into the form "08.070.838 / 0001-63.offset + 1".

To do this transformation you should use the preg_replace method like this:

<?php
$string = '08.070.838/0001-63.offset(1)';
$pattern = '/(".*?)\((\d*?)\)/g';
$replacement = '$1+$2';
echo preg_replace($pattern, $replacement, $string);
?>

You can see how this regex works here .

Explanation:

Capture pattern:
 - (.*?)\( - Captures all text up to ( .
 - (\d*?)\) - Captures all digits after ( to ) .

Replacement:
 - $1+ - Plays what is captured in group 1 with a + symbol after the content. Home  - $2 - Reproduces what is captured in group 2.

  

Would anyone know what is wrong with any of these \?

You are not using delimiters in your expression, you are putting it raw, you should always start with delimiter "/" to start the pattern and if you want to end with modifier you want. >

You can find the documentation on delimiters here

    
09.11.2017 / 12:49