常用表达式解析

假设属性abc的值为xyz。

1 ${abc} => xyz
2 $${abc} => ${abc}
3 $$${abc} => $xyz
4 $$$${abc} => $${abc}
5 $$$$${abc} => $$xyz

也就是从左往右两个$并一个$且以字符串输出,如果$和{}匹配才解析。

假设  属性"filename" 值为 "a brand new filename.txt"

1 ${filename:replaceAll('\..*', '')} => a brand new filename
2 ${filename:replaceAll('a brand (new)', '$1')} => new filename.txt
3 ${filename:replaceAll('XYZ', 'ZZZ')} => a brand new filename.txt
4 ${filename:replaceAll('brand (new)', 'somewhat $1')} => a somewhat new filename.txt
1 ${filename:replace('.', '_')} => a brand new filename_txt
2 ${filename:replace(' ', '.')} => a.brand.new.filename.txt
3 ${filename:replace('XYZ', 'ZZZ')} => a brand new filename.txt
4 ${filename:replace('filename', 'book')} => a brand new book.txt

假设 属性"str" 值为 "abc" 随机组

1 ${str:repeat(1)} => abc
2 ${str:repeat(2)} => abcabc
3 ${str:repeat(1,2)} => abc 或者 abcabc
4 ${str:repeat( ${str:length()} )} => abc 或者 abcabc 或者 abcabcabc

类型转换

1 ${fileSize:toNumber()}
2 ${fileSize:toNumber():toString()} 
3 ${fileSize:toDecimal()}  小数

假设 属性"myJson" 值如下:

 1 {
 2   "firstName": "John",
 3   "lastName": "Smith",
 4   "isAlive": true,
 5   "age": 25,
 6   "address": {
 7     "streetAddress": "21 2nd Street",
 8     "city": "New York",
 9     "state": "NY",
10     "postalCode": "10021-3100"
11   },
12   "phoneNumbers": [
13     {
14       "type": "home",
15       "number": "212 555-1234"
16     },
17     {
18       "type": "office",
19       "number": "646 555-4567"
20     }
21   ],
22   "children": [],
23   "spouse": null
24 }

取值

1 ${myJson:jsonPath('$.firstName')} => John
2 ${myJson:jsonPath('$.address.postalCode')} => 10021-3100
3 ${myJson:jsonPath('$.phoneNumbers[?(@.type=="home")].number')} => 212 555-1234
4 ${myJson:jsonPath('$.phoneNumbers')} => [{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]
5 ${myJson:jsonPath('$.missing-path')} => empty
6 ${myJson:jsonPath('$.bad-json-path..')} => exception bulletin

删除值

1 ${myJson:jsonPathDelete('$.firstName')} => {"lastName":"Smith","age":25,"address":{"streetAddress":"21 2nd Street","city":"New York","state":"NY","postalCode":"10021-3100"},"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]}
2 ${myJson:jsonPathDelete('$.missing-path')} => Returns original JSON document

添加[]值

1 ${myJson:jsonPathAdd('$.nicknames', 'Jimmy')} =>{"firstName":"James", lastName":"Smith", "age":25, "voter":true, "height":6.1, "address":{"streetAddress":"21 2nd Street", "city":"New York", "state":"NY", "postalCode":"10021-3100"}, "phoneNumbers":[{"type":"home", "number":"212 555-1234"}, {"type":"office", "number":"646 555-4567"}],"nicknames":["Jimmy"]}
2 ${myJson:jsonPathAdd('$.missingpath', 'Jimmy')} => Returns original JSON document with no modifications
3 ${myJson:jsonPathAdd('$.firstName', 'Jimmy')} => empty

添加对象值

1 ${myJson:jsonPathPut('$','middlename','Turon')} => {"firstName":"James", lastName":"Smith", "middlename": "Turon", "age":25, "voter":true, "height":6.1, "address":{"streetAddress":"21 2nd Street", "city":"New York", "state":"NY", "postalCode":"10021-3100"}, "phoneNumbers":[{"type":"home", "number":"212 555-1234"}, {"type":"office", "number":"646 555-4567"}]}

改变

1 ${myJson:jsonPathSet('$.firstName', 'James')} =>{"firstName":"James", lastName":"Smith", "age":25, "voter":true, "height":6.1, "address":{"streetAddress":"21 2nd Street", "city":"New York", "state":"NY", "postalCode":"10021-3100"}, "phoneNumbers":[{"type":"home", "number":"212 555-1234"}, {"type":"office", "number":"646 555-4567"}]}
2 ${myJson:jsonPathSet('$.missingpath', 'James')} => Returns original JSON document

判断ifElse

假设 属性"filename" 值"a brand new filename.txt";属性"nullFilename" 值null;属性"bool" 值"true"

1 ${bool:ifElse('a','b')} => a
2 ${literal(true):ifElse('a','b')} => a
3 ${nullFilename:isNull():ifElse('file does not exist', 'located file')} => file does not exist
4 ${nullFilename:ifElse('found', 'not_found')} => not_found
5 ${filename:ifElse('found', 'not_found')} => not_found
6 ${filename:isNull():not():ifElse('found', 'not_found')} => found

截取属性字符串

设 属性"filename" 值"a brand new filename.txt";

 1 #substring
 2 ${filename:substring(0,1)} => a
 3 ${filename:substring(2)} => brand new filename.txt
 4 ${filename:substring(12)} => filename.txt
 5 ${filename:substring( ${filename:length():minus(2)} )} => xt
 6 #substringBefore 第一个匹配开始
 7 ${filename:substringBefore('.')} => a brand new filename
 8 {filename:substringBefore(' ')} => a
 9 ${filename:substringBefore(' n')} => a brand
10 ${filename:substringBefore('missing')} => a brand new filename.txt
11 #substringBeforeLast 最后一个匹配开始
12 ${filename:substringBeforeLast('.')} => a brand new filename
13 ${filename:substringBeforeLast(' ')} => a brand new
14 ${filename:substringBeforeLast(' n')} => a brand
15 ${filename:substringBeforeLast('missing')} => a brand new filename.txt
16 #substringAfter 截取后面
17 ${filename:substringAfter('.')} => txt
18 ${filename:substringAfter(' ')} => brand new filename.txt
19 {filename:substringAfter(' n')} => ew filename.txt
20 ${filename:substringAfter('missing')} => a brand new filename.txt
21 #substringAfterLast
22 ${filename:substringAfterLast('.')} => txt
23 ${filename:substringAfterLast(' ')} => filename.txt
24 ${filename:substringAfterLast(' n')} => ew filename.txt
25 ${filename:substringAfterLast('missing')} => a brand new filename.txt
1 ${filename:append('.gz')} => "a brand new filename.txt.gz"
2 ${filename:prepend('1')}  => "1a brand new filename.txt"

 

获取值

 ${UUID()}

${ip()}

${hostname()}

isNull

${filename:isNull()} 返回 false 或 true。这种格式也可以写成${'filename':isNull()}

${filename:notNull()} 同上用法,判断相反。

更多详见文档

 

posted @ 2021-07-28 15:25  木章  阅读(233)  评论(0)    收藏  举报