-2

Let's say I have a string Str1 -> "[18.01,19.023],[20.0289,20.9273],[32.474742,284.383732]"

This is like a set of coordinates of type [x,y] and I want to extract all the "x" and "y" and get these in columns in a dataframe.

How can I do this?

I tried stri_locate_all,regexpr and other commands... When I use [(.*)] as the regex to fetch everything within brackets I end up reading everything from first to last bracket as a string instead of all possible occurrences (i.e. 18.01,19.023],[20.0289,20.9273],[32.474742,284.383732)..

2
  • .* is greedy, use .*? (lazy, non-greedy, reluctant...). Commented Jul 9 at 12:05
  • the string looks like valid json aside from a containing square bracket, so it can directly be a data.frame without regex paste0('[',Str1,']') |> jsonlite::fromJSON() |> data.frame()
    – Nir Graham
    Commented Jul 9 at 13:07

0

Browse other questions tagged or ask your own question.