Regex for extracting comma delimited numbers in brackets

Vitaly

String:

lorem ipsum 999
[id:284,286]
[id:28]

Block in brackets may contain a lot of numbers.

Regex:

\[id:(\d+)(,\d+)*\]

What I'd like to see:

284
286
28

Solution using PHP:

preg_match_all('/\[id:(.*)\]/', $input, $ids);
if (strpos($ids[1][0], ',')) {
    $ids = explode(',', $ids[1][0]);
    foreach ($ids as $id) {
        echo $id . "\n";
    }
} else {
    echo $ids[1][0];
}

But is it possible using regex without explode()?

Wiktor Stribiżew

The explode way is perhaps the best. Unfortunately, PCRE does not remember repeated groups, thus, you either do it in 2 steps (with the explode), or use a \G based regex. Here is a safer regex than the one you are using (if there are no spaces in between the numbers):

$input = "lorem ipsum 999 [id:284,286] [id:28]"; 
preg_match_all('~\[id:([\d,]*)]~', $input, $ids);
foreach ($ids[1] as $id) {
    print_r(explode(',', $id)) . PHP_EOL;
}

See the IDEONE demo

The '~\[id:([\d,]*)]~' regex matches [id: and then matches and captures into Group 1 zero or more (due to * 0+ occurrences quantifier) digits (\d) or ,s.

If you need a one-regex solution, in PHP, if you process individual strings, you can make use of a \G based regex that you can leverage to set up the leading boundary and then match the consecutive numbers:

'~(?:\[id:|(?!^)\G,)\K\d+~'

See the regex demo and this IDEONE demo:

$re = '~(?:\[id:|(?!^)\G,)\K\d+~'; 
$strs = array("lorem ipsum 999", "[id:284,286]", "[id:28]"); 
foreach ($strs as $s) {
    preg_match_all($re, $s, $matches);
    print_r($matches[0]);
}

Pattern details:

  • (?:\[id:|(?!^)\G,) - match the [id: literal character sequence or the end of each successful match with (?!^)\G with a comma after it
  • \K - omit the matched value
  • \d+ - only match 1+ digits

If there can be whitespace between the digits, add \s* after (and perhaps, before) the comma.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex for extracting comma delimited numbers in brackets

From Dev

Make Email Regex Comma Delimited

From Dev

Regex match for comma delimited string

From Dev

Regex matching comma delimited strings

From Dev

Regex for extracting nested parameters in brackets

From Dev

Regex split with comma but not in Square Brackets and not in Brackets

From Dev

regex for letters or numbers in brackets

From Dev

Replacing numbers on a comma delimited result on MYSQL

From Dev

Replacing numbers on a comma delimited result on MYSQL

From Java

Swift - Regex - Extract numbers in brackets divided by comma(1,1)(2,3)

From Dev

regex for numbers comma and space

From Dev

Javascript RegEx for a comma delimited list of file paths

From Dev

Java Regex to match comma delimited list of interfaces

From Dev

RegEx to match words in comma delimited list

From Dev

Regex: Comma delimited string(suburb,state,postcode)

From Dev

php regex match a comma delimited list of integers

From Dev

RegEx to match words in comma delimited list

From Dev

Split string delimited by comma without respect to commas in brackets

From Dev

extracting a word between brackets using regex pattern

From Dev

RegEx: Why start a comma-delimited string with a comma?

From Dev

JavaScript regex for numbers with brackets and commas

From Dev

Extracting a number with dot and comma using regex

From Dev

regex comma separated values inside square brackets

From Dev

regex to remove comma and space inside of square brackets

From Dev

Implementing regex in comma separated numbers

From Dev

Convert a comma delimited list of numbers to a hyphenated list or range

From Dev

Javascript: best way to search for numbers in comma delimited strings

From Dev

Python regex to capture a comma-delimited list of items

From Java

Regex with comma delimited string seperated by pipe sign in .NET

Related Related

  1. 1

    Regex for extracting comma delimited numbers in brackets

  2. 2

    Make Email Regex Comma Delimited

  3. 3

    Regex match for comma delimited string

  4. 4

    Regex matching comma delimited strings

  5. 5

    Regex for extracting nested parameters in brackets

  6. 6

    Regex split with comma but not in Square Brackets and not in Brackets

  7. 7

    regex for letters or numbers in brackets

  8. 8

    Replacing numbers on a comma delimited result on MYSQL

  9. 9

    Replacing numbers on a comma delimited result on MYSQL

  10. 10

    Swift - Regex - Extract numbers in brackets divided by comma(1,1)(2,3)

  11. 11

    regex for numbers comma and space

  12. 12

    Javascript RegEx for a comma delimited list of file paths

  13. 13

    Java Regex to match comma delimited list of interfaces

  14. 14

    RegEx to match words in comma delimited list

  15. 15

    Regex: Comma delimited string(suburb,state,postcode)

  16. 16

    php regex match a comma delimited list of integers

  17. 17

    RegEx to match words in comma delimited list

  18. 18

    Split string delimited by comma without respect to commas in brackets

  19. 19

    extracting a word between brackets using regex pattern

  20. 20

    RegEx: Why start a comma-delimited string with a comma?

  21. 21

    JavaScript regex for numbers with brackets and commas

  22. 22

    Extracting a number with dot and comma using regex

  23. 23

    regex comma separated values inside square brackets

  24. 24

    regex to remove comma and space inside of square brackets

  25. 25

    Implementing regex in comma separated numbers

  26. 26

    Convert a comma delimited list of numbers to a hyphenated list or range

  27. 27

    Javascript: best way to search for numbers in comma delimited strings

  28. 28

    Python regex to capture a comma-delimited list of items

  29. 29

    Regex with comma delimited string seperated by pipe sign in .NET

HotTag

Archive