Search code examples
phpregexbbcode

Parse number in BBCode


I have a BBCode quote tag that is formatted like this:

[quote=Username;123456]

The delimiter is always there. How can I only match the digits with a regular expression (PHP)?


Solution

  • One way (among others):

    \[[^\d\[\]]+(\d+)\]
    

    See a demo on regex101.com.


    Broken down, this says:

    \[         # match an open bracket
    [^\d\[\]]+ # match anything not brackets or digits
    (\d+)      # capture digits to group $1
    \]         # match a closing bracket
    

    Your digits will be in group $1.