How to capture a variably-long, comma-seperated list of strings via Regex?

le_me

I'm using ruby 2.0 and its regex engine.

Given the following string:

str = "one: two, three: four"

The list's length is variable (from 0 to infinite). How do I capture it's entries? So in this example the regex should match:

[1]:"one: two", [2]:"three: four"

This is what I came up with so far:

/((\w+:\s\w+),?)*/

but it only gives me:

 => #<MatchData "one: two," 1:"one: two," 2:"one: two"> 

What am I doing wrong?

falsetru

You don't need regular expression. Use String#split:

str = "one: two, three: four"
str.split(', ') # => ["one: two", "three: four"]

Using regular expression:

str.split(/, /) # => ["one: two", "three: four"]

str.scan(/[^,]+/) # => ["one: two", " three: four"]
str.scan(/[^,]+/).map &:strip # => ["one: two", "three: four"]

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 comma seperated list

From Dev

Regex expression - one or multiple strings seperated by comma

From Dev

Removing value from a comma seperated list with Regex

From Dev

Regex to allow a comma seperated list of codes

From Dev

Counting how many comma seperated strings in an input

From Dev

How to display comma seperated list into table in EmberJS?

From Dev

How to Print Unicode Strings Variably?

From Dev

How to combine value os list object in to a single string seperated by comma

From Dev

How to seperate comma seperated values in List Array as a single Datarow

From Dev

How to Match a Comma Seperated List and End with a Different Character

From Dev

Python list of strings seperated values incorrectly, how to merge broken values?

From Dev

Javascript regex for a comma seperated sequence of numbers

From Dev

Regex for word space comma seperated numbers

From Dev

regular expression for multiple search in list seperated by comma

From Dev

To get records in comma seperated List LINQ to Entities

From Dev

Creating multiple empty list seperated by comma

From Dev

Creating a Comma Seperated List from a subquery in SQL

From Dev

Record the types of a comma seperated list of identifiers?

From Dev

Python regex to capture a comma-delimited list of items

From Dev

How do I concatenate a string to all the members inside a comma seperated list in SQL Server 2008?

From Dev

How to get Bootstrap multiselect dropdown list value as comma seperated string when a POST method is invoked

From Dev

How can I transform a string of comma and brackets seperated list into another string?

From Dev

How to store comma seperated string into a table

From Dev

how to convert tuple to comma seperated string in pig

From Dev

how to split a comma seperated field in oracle?

From Dev

How to convert array in php to comma seperated string?

From Dev

How to substitution into word into double quotations and seperated by comma?

From Dev

how to store data seperated with comma in php

From Dev

Matching two comma seperated strings in Python and correct position counts

Related Related

  1. 1

    RegEx for comma seperated list

  2. 2

    Regex expression - one or multiple strings seperated by comma

  3. 3

    Removing value from a comma seperated list with Regex

  4. 4

    Regex to allow a comma seperated list of codes

  5. 5

    Counting how many comma seperated strings in an input

  6. 6

    How to display comma seperated list into table in EmberJS?

  7. 7

    How to Print Unicode Strings Variably?

  8. 8

    How to combine value os list object in to a single string seperated by comma

  9. 9

    How to seperate comma seperated values in List Array as a single Datarow

  10. 10

    How to Match a Comma Seperated List and End with a Different Character

  11. 11

    Python list of strings seperated values incorrectly, how to merge broken values?

  12. 12

    Javascript regex for a comma seperated sequence of numbers

  13. 13

    Regex for word space comma seperated numbers

  14. 14

    regular expression for multiple search in list seperated by comma

  15. 15

    To get records in comma seperated List LINQ to Entities

  16. 16

    Creating multiple empty list seperated by comma

  17. 17

    Creating a Comma Seperated List from a subquery in SQL

  18. 18

    Record the types of a comma seperated list of identifiers?

  19. 19

    Python regex to capture a comma-delimited list of items

  20. 20

    How do I concatenate a string to all the members inside a comma seperated list in SQL Server 2008?

  21. 21

    How to get Bootstrap multiselect dropdown list value as comma seperated string when a POST method is invoked

  22. 22

    How can I transform a string of comma and brackets seperated list into another string?

  23. 23

    How to store comma seperated string into a table

  24. 24

    how to convert tuple to comma seperated string in pig

  25. 25

    how to split a comma seperated field in oracle?

  26. 26

    How to convert array in php to comma seperated string?

  27. 27

    How to substitution into word into double quotations and seperated by comma?

  28. 28

    how to store data seperated with comma in php

  29. 29

    Matching two comma seperated strings in Python and correct position counts

HotTag

Archive