npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aliyun-sls/regexp-util

v0.0.6

Published

## 生成正则

Downloads

10

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: 时间格式校验是否通过