ここでは、文字列関数の構文、説明、パラメーター、戻り値、例について説明します。
substr
- 構文:
substr(s, i, j)
- 説明
文字列を部分的に抽出するには、この関数を呼び出します。
- パラメーター
- s: 抽出元のソース文字列。
- i: 1 からはじまる、ソース文字列の抽出開始位置。 値が -1 の場合は、文字列の右端の文字を指定します。 Data type : 整数。
- j: 1 からはじまる、ソース文字列の抽出終了位置。 値が -1 の場合は、文字列の右端の文字を指定します。 Data type : 整数。
- 戻り値
この関数はサブ文字列
s
のs[i, j]
を返します。 - 例
if eq(substr($uri, -5, -1), '.m3u8') { say(concat($uri, ' is .m3u8')) } uri_len = len($uri) if eq(substr($uri, -5, uri_len), '.m3u8') { say(concat($uri, ' is .m3u8')) } Note: the two methods that are used to determine whether a file is an M3U8 file
concat
- 構文:
concat(s1, ...)
- 説明
文字列を連結するには、この関数を呼び出します。
- パラメーター
s1: 連結される最初の文字列。 複数の文字列を指定することができます。
- 戻り値
この関数は連結文字列を返します。
- 例
if eq(substr($uri, -5, -1), '.m3u8') { say(concat($uri, ' is .m3u8')) } uri_len = len($uri) if eq(substr($uri, -5, uri_len), '.m3u8') { say(concat($uri, ' is .m3u8')) } Note: the two methods that are used to determine whether a file is an M3U8 file
upper
- 構文:
upper(s)
- 説明
文字列を大文字に変換するには、この関数を呼び出します。
- パラメーター
s: 変換されるソース文字列。
- 戻り値
この関数は、指定された
s
パラメーターを大文字で返します。 - 例
mystr = 'Hello, Dsl' say(upper(mystr)) say(lower(mystr)) Output: HELLO,DSL hello, dsl
lower
- 構文:
lower(s)
- 説明
文字列を小文字に変換するには、この関数を呼び出します。
- パラメーター
s: 変換されるソース文字列。
- 戻り値
この関数は、
s
パラメーターで指定された文字列を小文字で返します。 - 例
mystr = 'Hello, Dsl' say(upper(mystr)) say(lower(mystr)) Output: HELLO,DSL hello, dsl
len
- 構文:
len(s)
- 説明
この関数を呼び出すと、文字列の長さが返されます。
- パラメーター
s: 長さを返すソース文字列。
- 戻り値
この関数は、
s
パラメーターで指定されるソース文字列の長さ (整数) を返します。 - 例
mystr = 'Hello, Dsl' say(upper(mystr)) say(lower(mystr)) Output: HELLO,DSL hello, dsl
byte
- 構文:
byte(c)
- 説明
この関数を呼び出すと、文字の ASCII 値が返されます。
- パラメーター
c: ASCII 値を返す文字。 指定できる文字は 1 つのみです。
- 戻り値
この関数は、対応する ASCII 値 (数値型) を返します。
- 例
say(byte('a')) say(byte('A')) Output: 97 65
match_re
- 構文:
match_re(s, p [, o])
- 説明
PCRE エンジンを使用して正規表現を照合するには、この関数を呼び出します。
- パラメーター
- s: 一致するソース文字列。 Data type: 文字列。
- p: 正規表現。 Data type: 文字列。
- (任意) o : 正規表現エンジンパラメーター。 Data type: 文字列。
- 戻り値
この関数は、成功の場合は
true
を返し、失敗の場合はfalse
を返します。 - 例
url = concat('http://', $host, $uri) m1 = match_re(url, 'http://. *\.dslex\.com/.*') m2 = match_re(url, '^http://. *\.alibaba\.com\.cn/. *\.d\\.html(\?. *)? $') m3 = match_re(url, '^http://. *.test.dslex.com/. *\.d\.html(\?. *)? $') m4 = match_re(url, '^http://. *\.alibaba\.com\.cn/zt_d/') m5 = match_re(url, '^http://tech.alibaba.com.cn/zt_d/we2015/?$') m6 = match_re($args, 'from=wap1$') m7 = match_re($args, 'from=comos1$') if and(m1, or(m2, m3), not(m4), not(m5), or(not(m6), not(m7))) { add_rsp_header('USER-DEFINED-1', 'hit1') add_rsp_header('USER-DEFINED-2', 'hit2') }
capture_re
- 構文:
capture_re(s, p [,init])
- 説明
この関数を呼び出すと、文字列のマッチをキャプチャし、キャプチャされたサブ文字列が返されます。 文字列の照合には PCRE エンジンを使用します。
- パラメーター
- s: 照合するソース文字列。 Data type: 文字列。
- p: 照合の正規表現。 Data type: 文字列。
- init: 1 からはじまる、照合の開始位置。 Data type: 整数。
- 戻り値
この関数は、成功の場合は一致するサブ文字列をディクショナリ型で返し、失敗の場合は 空のディクショナリを返します。
- 例
pcs = capture_re($request_uri,'^/([^/]+)/([^/]+)([^?] +)\?(.*)') sec1 = get(pcs, 1) sec2 = get(pcs, 2) sec3 = get(pcs, 3) if or(not(sec1), not(sec2), not(sec3)) { add_rsp_header('X-TENGINE-ERROR', 'auth failed - missing necessary uri set') exit(403) } digest = md5(concat(sec1, sec3)) if ne(digest, sec2) { add_rsp_header('X-TENGINE-ERROR', 'auth failed - invalid digest') exit(403) }
gsub_re
- 構文:
gsub_re(subject, regex, replace [,option])
- 説明
この関数を呼び出すと、一致した文字列をすべて置き換え、置き換え後の文字列が返されます。 文字列照合には PCRE エンジンを使用します。
- パラメーター
- subject: 照合するソース文字列。 Data type: 文字列。
- regex: 照合の正規表現。 Data type: 文字列。
- replace: 置き換える文字列。 Data type: 文字列。 一致するサブ文字列を使用して
replace
パラメーターを指定します。- $0:
regex
と一致するすべてのサブ文字列を指定します。 - $N:
regex
の N 番目の括弧付きのサブ式()
に一致するサブ文字列を指定します。
- $0:
- (任意) option: 正規表現エンジンパラメーター。 Data type: 文字列。
- 戻り値
この関数は、指定された
subject
パラメーターの、指定されたregex
パラメーターと一致するすべてのサブ文字列を、指定されたreplace
パラメーターを置き換え、置き換え後の文字列を返します。 - 例
subject = 'Hello, Es' regex = '([a-zA-Z])[a-z]+' replace = '[$0,$1]' add_rsp_header('X-DEBUG-GSUB-RE', gsub_re(subject, regex, replace)) Output: X-DEBUG-GSUB-RE: [Hello,H], [Es,E]
- こちらもご参照ください。
PCRE 構文: https://www.pcre.org/current/doc/html/pcre2syntax.html
split
- 構文:
split(s [,sep])
- 説明
この関数を呼び出すと、文字列がサブ文字列の配列に分割され、新しい配列が返されます。
- パラメーター
- s: 分割するソース文字列。 Data type: 文字列。
- sep: 文字列の分割に使用する区切り記号。 Data type: 文字列。
- 戻り値
この関数は、"key=value" 要素の配列をディクショナリ型で返します。 1 からはじまる数値が
key
の値として使用されます。 例:[1]=xx, [2]=yysep
パラメーターが指定されていない場合、文字列は空白文字で区切られます。 - 例
if $arg_from { t = split($arg_from, ',') if get(t, 1) { say(concat('[1]=', get(t, 1))) } if get(t, 2) { say(concat('[2]=', get(t, 1))) } } Request: ? from=xx1,xx2,xx3 Response: [1]=xx1 [2]=xx1
split_as_key
- 構文:
split_as_key(s [,sep])
- 説明
この関数を呼び出すと、文字列がサブ文字列の配列に分割され、新しい配列が返されます。
- パラメーター
- s: 分割するソース文字列。 Data type: 文字列。
- sep: 文字列の分割に使用する区切り記号。 Data type: 文字列。
- 戻り値
split()
関数の戻り値に似ています。key
が分割された各要素に基づいて名前がつけられている点が異なります (element 1
->element 2
) 。 - 例
def echo_each(k, v, u) { s = concat(k, '=', v, ' u=', get(u, 1)) say(s) } if $arg_from { t = split_as_key($arg_from, ',') foreach(t, echo_each, ['hi,dsl']) } Request: ? from=xx1,xx2,xx3 Response: xx2=xx2 u=hi,dsl xx1=xx1 u=hi,dsl xx3=xx3 u=hi,dsl
tohex
- 構文:
tohex(s)
- 説明
文字列を六桁の文字列に変換するには、この関数を呼び出します。
- パラメーター
s: 変換する文字列。
- 戻り値
この関数は、指定された
s
パラメーターから変換された 16 進数の文字列を返します。 - 例
digest = sha1('xxxx') add_rsp_header('X-DSL-TOHEX', tohex(digest)) Note: Add a response header. X-DSL-TOHEX: 4ad583af22c2e7d40c1c916b2920299155a46464
tostring
- 構文:
tostring(a)
- 説明
任意の型のデータを文字列に変換するには、この関数を呼び出します。
- パラメーター
a: 変換されるデータ。 Data type: 任意の型。
- 戻り値
この関数は、指定された
a
パラメーターから変換された文字列を返します。 - 例
s = tostring(123) add_rsp_header('X-DSL-TOSTRING', s) Note: Add a response header. X-DSL-TOSTRING: 123
tochar
- 構文:
tochar(n1, n2, ...)
- 説明
- 1 つ以上の内部整数 (ASCII 値) を文字列に変換するには、この関数を呼び出します。 例:48 は文字の "0" に相当します。
- 返される文字列の長さは、指定されたパラメーターの数によって決まります。
- パラメーター
nX: 変換される整数。 複数の整数を指定することができます。
- 戻り値
この関数は、変換後の文字列を返します。
- 例
add_rsp_header('X-DSL-TOCHAR', tochar(97)) add_rsp_header('X-DSL-TOCHAR', tochar(97, 98), true) Output: Add the following response headers. X-DSL-TOCHAR: a X-DSL-TOCHAR: ab if $arg_filename { hn = 'Content-Disposition' add_rsp_header('Content-Disposition', concat('attachment;filename=', tochar(34), filename, tochar(34))) add_rsp_header(hn, hv) } Output: Add the response header. Content-Disposition: attachment;filename="The value of the filename parameter"