0

I have JS file and I need to parse that array in it:

something: ['Prefix.All','Ignore.Me','Prefix.Another']

I need to get all elements from "something" array with defined prefix. Now I can get it, if array will contain only one element. My regexp:

String: something: ['Prefix.All']
Can get element with:
/something\s*:\s*\[['"](Prefix[a-zA-Z0-9.]+)['"]]/

But how to find many elements in array? And how to find more than one "something" array in file?

6
  • Is your something always going to start with Prefix.All I would suggest you to split it by comma... Commented Jan 15, 2013 at 7:37
  • I need to find "Prefix.***[.***]" here. Not Prefix.All Commented Jan 15, 2013 at 7:44
  • What regex engine are you using for this? Unless it's .NET, you can't do it with a single regex. Commented Jan 15, 2013 at 7:56
  • 2
    uh, take string, find first [, find last ]. take substring in [ ], split it by ,. find all substrings, that have prefix Prefix. Why you think, that regexp can really helps you? Commented Jan 15, 2013 at 8:08
  • In that case I will find: required:[..], anything:[..], another:[..] e.t.c Commented Jan 15, 2013 at 8:11

2 Answers 2

3

In PHP you need to do it in two steps (which is probably better anyway - cramming everything into a single regex isn't always the best idea).

First, match the array you're looking for. Note that this regex requires that there are no brackets between the delimiters [ and ], so nested arrays or strings that contain brackets will cause the regex to fail (as in "match the wrong text"):

if (preg_match('/\bsomething: \[([^[\]]*)\]/', $subject, $regs)) {
    $result = $regs[1];
}

This will find the first occurrence of a string like something: ['Prefix.All','Ignore.Me','Prefix.Another'] and put 'Prefix.All','Ignore.Me','Prefix.Another' into $result.

Then you can get all the matches from that:

preg_match_all('/\bPrefix[^\']*/', $result, $final, PREG_PATTERN_ORDER);
$final = $final[0];
Sign up to request clarification or add additional context in comments.

Comments

0

In regex you can group a token with (), and ask for it many times with *.

So: (something)*would give you every instance of the word 'something'.

Further: (__your__token__here__)* finds every instance of your token.

Paste some of your array here: http://gskinner.com/RegExr/ and use the regex builder to test your regex

Does that help?

4 Comments

I tried simple example. Find create[\d] in that string: "create2 create3 create4". But ()* don't work. Here is my repexp: /(create[\d])*/ . It can't do that. Mistake?
See edit - I always use an online regex tester to test what I want to do until I get it right - without seeing your array its a bit hard to figure out what you are trying to achieve. For your example, you just need (create[\d])
(something)* matches "", "something", "somethingsomething" etc., but it doesn't do at all what the OP wants.
Not really. In his example, two of the "tokens" he's looking for are separated by a token he does not want to match, so * wouldn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.