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
"[one] abc [two] ef [lunch time]" .. the regex should match "one", "two" and "lunch time".
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :)
<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 :)
ASKER
Infact marchent's solution worked with a minor ammendment:
\[([^\]]*)\]
(removing the initial backslash)
thanks
\[([^\]]*)\]
(removing the initial backslash)
thanks
ASKER
forward slash sorry
ASKER
thanks
Open in new window