Link to home
Start Free TrialLog in
Avatar of paddycobbett
paddycobbett

asked on

I need a REGEX to match all strings in square brackets

I need a regex to match all strings inside square brackets, i.e:

"[one]  abc [two] ef [lunch time]"  .. the regex should match "one", "two" and "lunch time".

Thanks
Avatar of ghostdog74
ghostdog74


/\[(.*?)\]/g;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of marchent
marchent
Flag of Bangladesh image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paddycobbett

ASKER

Hello and thanks for the reponses. Unfortunately neither of the 2 solutions worked for me, both returning empty results, i'm not sure if it's related to the regex engine that coldfusion uses(??), have not worked with regex before.

<cfset path="  [hello] [Good morning]">

<cfset arrTitles = REMatch("/\[([^\]]*)\]/", path) />
<cfdump var="#arrTitles#">

<cfset arrTitles = REMatch("/\[(.*?)\]/g", path) />
<cfdump var="#arrTitles#">


Let me point out however that the below regex was provided by a previous post, and only didn't match when the text had spaces imbetween the square brackets, so with the above sample input it DID match "hello" but NOT "good morning" since it has a space in it. Can the below regex be ammended to handle spaces?

<cfset arrTitles = REMatch("\[(\w+)\]", path) />
<cfdump var="#arrTitles#">

thanks :)
Infact marchent's solution worked with a minor ammendment:

\[([^\]]*)\]

(removing the initial backslash)

thanks
forward slash sorry
thanks