@aliyun-sls/regexp-util
v0.0.6
Published
## 生成正则
Downloads
14
Readme
SLS RegExp Util (日志服务正则工具库)
生成正则
import { genLogRegex } from '@aliyun-sls/regexp-util'
const params1 = {
prefix: '',
content: '127.0.0.1',
suffix: ' - Alice [2024-06-25T19:22:23+08:00] "DELETE /request/path-9/file-1 HTTP/1.1" 168 82684 403 31804 www.test1.com www.test5.com "Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1"',
}
const result1 = await genLogRegex(params1)
// { regex: '(\\S+).*' }
const params2 = {
prefix: ' - ',
content: 'Alice',
suffix: ' [2024-06-25T19:22:23+08:00] "DELETE /request/path-9/file-1 HTTP/1.1" 168 82684 403 31804 www.test1.com www.test5.com "Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1"',
}
const result2 = await genLogRegex(params2)
// { regex: '\\s-\\s(\\w+).*' }
入参:
- content: 需要生成正则的内容
- prefix: content 之前的字符串(不包含已经生成正则的部分)
- suffix: content 之后的字符串
结果:
- regex: content 的正则格式
校验正则
import { checkLogRegex } from '@aliyun-sls/regexp-util'
const params = {
LogContent: '127.0.0.1 - Alice [2024-06-25T19:22:23+08:00] "DELETE /request/path-9/file-1 HTTP/1.1" 168 82684 403 31804 www.test1.com www.test5.com "Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1"',
BeginRegex: '.*',
LogRegex: '(\\S+)\\s-\\s(\\w+)\\s\\[([^]]+).*'
}
const result = await checkLogRegex(params)
// {
// result: [ '127.0.0.1', 'Alice', '2024-06-25T19:22:23+08:00' ],
// number: 3
// }
入参:
- LogContent: 日志内容
- BeginRegex: 单行日志固定使用
.*
即可,多行日志的话使用行首正则表达式 - LogRegex: 用于提取字段的正则表达式
结果:
- result: 提取出来的字段
- number: 提取出来的字段个数
生成行首正则
import { genBeginRegex } from '@aliyun-sls/regexp-util'
const params = {
logBegin: '127.0.0.1 - Alice [2024-06-25T19:22:23+08:00] \n "DELETE /request/path-9/file-1 HTTP/1.1" 168 82684 403 31804 www.test1.com www.test5.com "Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1"'
}
const result = await genBeginRegex(params)
// { regex: '\\d+\\.\\d+\\.\\d+\\.\\d+\\s-\\s.*' }
入参:
- logBegin: 多行日志内容
结果:
- regex: 行首正则表达式
校验行首正则
import { checkBeginRegex } from '@aliyun-sls/regexp-util'
const multipleLogContent = '127.0.0.1 - Alice [2024-06-25T19:22:23+08:00] \n "DELETE /request/path-9/file-1 HTTP/1.1" 168 82684 403 31804 www.test1.com www.test5.com "Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1"'
const params1 = {
BeginRegex: '\\d+\\.\\d+\\.\\d+\\.\\d+\\s-\\s.*',
LogContent: multipleLogContent,
}
const result1 = await checkBeginRegex(params1)
// {
// lines: 1,
// position: [0],
// result: [
// '127.0.0.1 - Alice [2024-06-25T19:22:23+08:00] ',
// ' "DELETE /request/path-9/file-1 HTTP/1.1" 168 82684 403 31804 www.test1.com www.test5.com "Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1"'
// ]
// }
const params2 = {
BeginRegex: '\\d+\\.\\d+\\.\\d+\\.\\d+\\s-\\s.*',
LogContent: multipleLogContent + '\n' + multipleLogContent,
}
const result2 = await checkBeginRegex(params2)
// {
// lines: 2,
// position: [0, 2],
// result: [
// '127.0.0.1 - Alice [2024-06-25T19:22:23+08:00] ',
// ' "DELETE /request/path-9/file-1 HTTP/1.1" 168 82684 403 31804 www.test1.com www.test5.com "Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1"',
// '127.0.0.1 - Alice [2024-06-25T19:22:23+08:00] ',
// ' "DELETE /request/path-9/file-1 HTTP/1.1" 168 82684 403 31804 www.test1.com www.test5.com "Mozilla/5.0 (Windows NT 5.2; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1"'
// ]
// }
入参:
- BeginRegex: 行首正则表达式
- LogContent: 日志内容
结果:
- lines: 匹配到的日志条数
- position: 匹配行首正则表达式的行号,从0开始
- result: 分割后的每一行日志
生成时间格式
import { genTimeFormat } from '@aliyun-sls/regexp-util'
const params = {
timeValue: '2024-06-25 19:22:23'
}
const result = await genTimeFormat(params)
// { regex: '%Y-%m-%d %H:%M:%S' }
入参:
- timeValue: 时间字符串
结果:
- regex: 时间格式
校验时间格式
import { checkTimeFormat } from '@aliyun-sls/regexp-util'
const params = {
timeValue: '2024-06-25 19:22:23',
timeFormat: '%Y-%m-%d %H:%M:%S'
}
const result = await checkTimeFormat(params)
// { result: true }
入参:
- timeValue: 时间字符串
- timeFormat: 时间格式
结果:
- result: 时间格式校验是否通过