LoginSignup
1
1

More than 3 years have passed since last update.

【ColdFusion】正規表現で部分文字列を取得

Last updated at Posted at 2018-08-29

ColdFusionで正規表現を使って部分文字列を取得したい

調べたらこんな感じで取れるらしい。
照合された部分文字列の戻し

関数を作ってみた

<!--- 正規表現で部分文字列を取得する関数 --->
<CFFUNCTION name="REArray" returntype="array">
    <CFARGUMENT name="reg_ex" type="string" required="true">
    <CFARGUMENT name="target_str" type="string" required="true">

    <CFSET local.regex = REFindNoCase(reg_ex, target_str, 1, true)>
    <CFSET local.aResult = arrayNew(1)>
    <CFLOOP from="1" to="#arrayLen(local.regex.pos)#" index="i">
        <CFIF local.regex.pos[i] neq 0>
            <CFSET arrayAppend(local.aResult, mid(target_str, local.regex.pos[i], local.regex.len[i]))>
        </CFIF>
    </CFLOOP>

    <CFRETURN local.aResult>
</CFFUNCTION>

使用例

<CFSET aResult = REArray("^【(\d+)月(\d+)日.+】(.+)おまかせ御膳((.+))$", "【8月30日(木)】雑穀米220gおまかせ御膳(海老チリ&シューマイ)")>
<CFDUMP var="#aResult#">

こんな感じの配列を返します。
2018-08-29_175113.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1