1

I'm having a Regex issue that I'm hoping someone can help with.

I have the following type of string:

" name VARCHAR(11) NOT NULL, "

Of which I'm trying to extract only the length - in otherwords the numbers within the brackets.

I have looked at this: True regex for getting content from parentheses in square brackets but I'm just not figuring out how to make that work with my case.

I know that I could use str_replace to remove the unwanted data, but this is going to be messy and waste resources.

Can anyone help?

3 Answers 3

1

This should do it:

$matches = Array();
preg_match("/\w+\((\d+)\)/i", " name VARCHAR(11) NOT NULL, ", $matches);

$matches will now contain

Array
(
    [0] => VARCHAR(11)
    [1] => 11
)
Sign up to request clarification or add additional context in comments.

Comments

0

If all you are looking for is some digits between the brackets, then you could use some code similar to this -

$str = " name VARCHAR(11) NOT NULL, ";
$matches = array();
$pattern = '/\((\d+)\)/';
preg_match_all($pattern,$str,$matches);

This code uses the preg_match() function, which performs a regular expression match.

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

Comments

0

Try this pattern:

/.*\(([0-9]+)\).*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.