1

I'm having a problem with proper regex expression for my case. So i have this "text" which i need to convert in to the PHP array with find an replace function in my code editor

$grid['added'][0][qwer'] = 'asda';
$grid['added'][0][tzui'] = 'asda';
$grid['added'][1][sdfg'] = 'asda';
$grid['added'][2][ghjk'] = 'asda';
....
$grid['added'][4][nbmh'] = 'asda';
$grid['added'][666][fghz'] = 'asda';

desired output

$grid['added'][0]['qwer'] = 'asda';
$grid['added'][0]['tzui'] = 'asda';
$grid['added'][1]['sdfg'] = 'asda';
$grid['added'][2]['ghjk'] = 'asda';
....
$grid['added'][4]['nbmh'] = 'asda';
$grid['added'][666]['fghz'] = 'asda';

So far i'm trying to fetch all numbers inside square brackets, then add that apostrophe but editor doesn't find any thing for the following regex.

/\[[0-9]+\]/

So how should my regex look like so i can add that apostrophe to my "text" in order to have proper array structure for my code. If you need any additional information's, please let me know and i will provide. Thank you

3
  • Please share the code or at least let us know how you are using the regex. Commented Nov 3, 2016 at 10:01
  • Is your original text also missing the left apostrophe on the last key? Commented Nov 3, 2016 at 10:01
  • whell original text looked like this grid[added][0][qwer]:asda Commented Nov 3, 2016 at 10:02

1 Answer 1

2

Find:

\['?([a-zA-Z]+)'?\]

Replace:

['$1']

This finds any set of letters within square brackets, that may or may not have any quotes around it, and makes sure there are two quotes.

Sign up to request clarification or add additional context in comments.

2 Comments

And please up vote question, because i got - without any explanation :/
@Valor_, I agree that your question was understandable and didn't deserve a downvote. So I upvoted to compensate.

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.