0

I have this string:

'newProductsInfo: [[1] - $2 dollars,[2] New Product,[3] Hello,[4]Same, [5]Value]'

The word 'newProductsInfo', and a space ' ' always precede the array of strings. I want to be able to return

[1] - $2 dollars

[2] New Product

[3] Hello

[4]Same, [5]Value //should be returned since the comma is followed by a space ' ' 

in the Regex101 site.

Currently, using (?<=newProductsInfo: \[)[^,\]]+ only returns [1.

enter image description here

Edit: added possible return types in bubble plugin creator:

enter image description here

Prosy Arceno
  • 2,616
  • 1
  • 8
  • 32
  • _"I have this string"_ - The first thing to do is usually to figure out in what format the string is. I don't recognize it. Where does this string come from? Most properly specified formats already have parsers so you don't have to go regex on the string. It looks like a complicated format so you will _most probably_ parse it erroneously if you try it with regex - if not with the data you see Today, then with something that pops up Tomorrow. – Ted Lyngmo Mar 10 '23 at 14:57
  • @TedLyngmo I'm using it inside a [website builder](https://bubble.io), and in there I'm only allowed to return texts or a list of texts. aside from the object I have in my question, I'm also returning various other objects. Then again, I can only return it as a text. Limitations of the app made me use regex for recognizing patterns. – Prosy Arceno Mar 10 '23 at 15:01
  • May I suggest returning text formatted as JSON? You then have a very well defined format and support in most (all?) languages to parse it properly. – Ted Lyngmo Mar 10 '23 at 15:04
  • using JSON only displays something like [object Object] so it was useless for me. – Prosy Arceno Mar 10 '23 at 15:05
  • Then you used it wrong. You need to build the JSON object and convert it to a string before returning it. Do the opposite on the receiving side. – Ted Lyngmo Mar 10 '23 at 15:06
  • Is this a PHP problem, or a JS problem? – Nico Haase Mar 10 '23 at 15:41
  • @NicoHaase its a php problem – Prosy Arceno Mar 10 '23 at 15:47
  • @NicoHaase I need to make it work [here](https://regex101.com/r/dHZWQ8/1) – Prosy Arceno Mar 10 '23 at 15:48
  • @ProsyArceno How much time did you spend on trying to get JSON working? Did you give up when you noticed that your JSON object appeared as `[object Object]`? If you had just spent a few minutes extra there, I'm pretty sure you'd figure out how to make it appear as a fully parseable string - and all this regexing of your own format would be a non-issue. – Ted Lyngmo Mar 10 '23 at 15:55
  • If this is a PHP problem, please share the code invovled – Nico Haase Mar 10 '23 at 16:00
  • @TedLyngmo, I'm new to the web builder and newer to its plugin creator. But I am limited to the return of the plugin creator, see edit above – Prosy Arceno Mar 10 '23 at 16:03
  • It looks like they've tried to make it simple to juggle JSON objects back and forth. I suggest spending some time investigating that a bit more. Perhaps ask about how to do it properly in one of their own user forums (if they have any) – Ted Lyngmo Mar 10 '23 at 16:27
  • @TedLyngmo It's not so much a problem in the app but a problem in the return of the API I'm using. Mapping made it problematic because what supposedly should be returned as an array is being returned as a dictionary. What I'm currently doing is a workaround, which because of my limited knowledge of the app, may not be the most optimised. See [here](https://forum.bubble.io/t/extracting-return-of-a-plugin-and-saving-to-database/243599/3). I've gone past extracting the ids months ago. Now, I wanna get the names instead, so here I am. – Prosy Arceno Mar 10 '23 at 16:40
  • @ProsyArceno The JSON you showed in that question is far from valid JSON, so I'd take their answer with a grain of salt. What if you asked the same question, but with valid JSON? – Ted Lyngmo Mar 10 '23 at 16:49
  • @TedLyngmo that is the actual represented return from the API I'm currently using. See last edit a result from a call I made. – Prosy Arceno Mar 10 '23 at 16:57
  • 1
    Your latest addition to the question contains pictures though. It's pretty hard to validate those. Put the data _as-is_ in the question as text and I'll give it a go. – Ted Lyngmo Mar 10 '23 at 16:58

2 Answers2

2

Your pattern (was tagged JavaScript) only matches [1 because the negated character class [^,\]]+ can not cross matching a comma or ]

If you want the matches only, you can assert newProductsInfo: to the left followed by any character

Start the match with digits between square brackets up until either the next occurrence preceded by a comma, or the end of the string.

Using a lookbehind assertion (see the support) for it:

(?<=newProductsInfo: .*?)\[\d+].*?(?=,\[\d+]|$)

Regex demo

Edit

If you want to use PHP:

(?:newProductsInfo: \[|\G(?!^)),?\K[^,\n]+(?>,\h+[^,\n]+)*

Explanation

  • (?: Non capture group for the alternatives
    • newProductsInfo: \[ Match newProductsInfo: [
    • | Or
    • \G(?!^) Assert the current position at the end of the previous match, not at the start
  • ) Close the non capture group
  • ,?\K Match an optional comma and forget what is matched so far
  • [^,\n]+
  • (?>,\h+[^,\n]+)* Optionally repeat matching a comma, 1+ spaces and 1+ chars other than , or newline

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

Here is one approach using match():

var input = 'newProductsInfo: [[1] - $2 dollars,[2] New Product,[3] Hello,[4]Same, [5]Value]';
var matches = input.replace(/^.*?\[(?=\[\d+\])|\]$/g, "")
                   .split(/,(?=\[\d+\])/);
console.log(matches);

The call to replace() strips off the leading/trailing [ and ]. The call to split() splits the input string at every comma which is followed by a numbered bracket term such as [2].

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360