Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正则匹配转义字符相关问题 #603

Closed
6 tasks done
LingEmberMaple opened this issue Apr 18, 2024 · 4 comments
Closed
6 tasks done

正则匹配转义字符相关问题 #603

LingEmberMaple opened this issue Apr 18, 2024 · 4 comments

Comments

@LingEmberMaple
Copy link

一些验证

  • 搜索检查是否已经存在请求相同功能的问题/讨论,以避免重复创建问题
  • GKD 默认不包含任何规则, 如果您想要自定义规则, 您可以 查看文档
  • 1.我确认知晓并同意维护者直接关闭不符合 issue 规范的问题
  • 2.我确认知晓并同意维护者直接关闭不符合 issue 规范的问题
  • 3.我确认知晓并同意维护者直接关闭不符合 issue 规范的问题
  • 我已确保提供下列的日志和BUG描述及其复现步骤, 否则我同意维护者直接关闭问题

日志文件-无论什么问题不包含日志将会被直接关闭

log.zip

BUG描述(文字/截图/视频)

使用 matches\notMatches 操作符 且 值 包含转义符时,保存规则将忽略转义符

示例规则

{
  id: 'com.miui.securitycenter',
  name: '小米手机管家',
  groups: [
    {
      name: '功能类-高敏感权限自动确定',
      key: 1,
      desc: '勾选[我已知晓可能存在的风险]-10s后点击[确定]',
      enable: false,
      quickFind: true,
      activityIds: [
        'com.miui.permcenter.privacymanager.SpecialPermissionInterceptActivity'
      ],
      rules: [
        {
          key: 0,
          matches: [
            '@[checked=false] + [id="com.miui.securitycenter:id/intercept_warn_content_end"]'
          ],
          snapshotUrls: [
            'https://i.gkd.li/i/14965657'
          ],
          exampleUrls: [
            'https://m.gkd.li/57941037/e9672ccd-8dd1-4060-bdbe-52bb355d404f'
          ]
        },
        {
          key: 1,
          preKeys: [
            0
          ],
          matches: [
            '[text!~=".*\(\d*\)"][id="com.miui.securitycenter:id/intercept_warn_allow"]'
          ],
          snapshotUrls: [
            'https://i.gkd.li/i/14965656'
          ],
          exampleUrls: [
            'https://m.gkd.li/57941037/1f2a118a-db2a-448d-a95d-f10d746b72e3'
          ]
        }
      ]
    }
  ]
}

保存后规则如下

展开

{
  name: '功能类-高敏感权限自动确定',
  key: 0,
  desc: '勾选[我已知晓可能存在的风险]-10s后点击[确定]',
  enable: false,
  quickFind: true,
  activityIds: [
    'com.miui.permcenter.privacymanager.SpecialPermissionInterceptActivity'
  ],
  rules: [
    {
      key: 0,
      matches: [
        '@[checked=false] + [id="com.miui.securitycenter:id/intercept_warn_content_end"]'
      ],
      snapshotUrls: [
        'https://i.gkd.li/i/14965657'
      ],
      exampleUrls: [
        'https://m.gkd.li/57941037/e9672ccd-8dd1-4060-bdbe-52bb355d404f'
      ]
    },
    {
      key: 1,
      preKeys: [
        0
      ],
      matches: [
        '[text!~=".*(d*)"][id="com.miui.securitycenter:id/intercept_warn_allow"]'
      ],
      snapshotUrls: [
        'https://i.gkd.li/i/14965656'
      ],
      exampleUrls: [
        'https://m.gkd.li/57941037/1f2a118a-db2a-448d-a95d-f10d746b72e3'
      ]
    }
  ]
}

第33行由
'[text!~=".*\(\d*\)"][id="com.miui.securitycenter:id/intercept_warn_allow"]'
保存为
'[text!~=".*(d*)"][id="com.miui.securitycenter:id/intercept_warn_allow"]'
转义符被忽略

期望行为(文字/截图/视频)

规则第33行保存为'[text!~=".*\(\d*\)"][id="com.miui.securitycenter:id/intercept_warn_allow"]'且正常触发

实际行为(文字/截图/视频)

规则第33行保存为'[text!~=".*(d*)"][id="com.miui.securitycenter:id/intercept_warn_allow"]'

@lisonge
Copy link
Member

lisonge commented Apr 18, 2024

'[text!~=".*\\\\(\\\\d*\\\\)"][id="com.miui.securitycenter:id/intercept_warn_allow"]'

@lisonge lisonge closed this as completed Apr 18, 2024
@lisonge
Copy link
Member

lisonge commented Apr 18, 2024

首先你写的规则文件里的转译字符先被 json5 转义一次,然后被 gkd 的 string 类型转义一次,最后传递给 kotlin 的 Regex

所以你要写两次转义,换句话说,如果你想要正则匹配一个原始的 \ 字符

对与 kotlin 来说,要写成 Regex("\\\\").matches(text)

所以对与 gkd 来说,要写成 [text~="\\\\\"]

对与 JavaScript/JSON/JSON5 来说,要写成 [text~="\\\\\\\\"]

matches : '[text~="\\\\\\\\"]'

如果你的规则文件是由 https://github.com/gkd-kit/subscription-template 构建

你可以使用 JavaScript 的原始字符串语法去除一次转义,这样只需要写一次转义

matches : String.raw`[text~="\\\\"]`

@lisonge lisonge changed the title 规则保存时转义符被忽略 规则里选择器的正则转义字符相关问题 Apr 18, 2024
@lisonge lisonge pinned this issue Apr 18, 2024
@lisonge lisonge changed the title 规则里选择器的正则转义字符相关问题 规则里选择器的正则匹配转义字符相关问题 Apr 18, 2024
@lisonge lisonge changed the title 规则里选择器的正则匹配转义字符相关问题 正则匹配转义字符相关问题 Apr 18, 2024
@LingEmberMaple
Copy link
Author

首先你写的规则文件里的转译字符先被 json5 转义一次,然后被 gkd 的 string 类型转义一次,最后传递给 kotlin 的 Regex

所以你要写两次转义,换句话说,如果你想要正则匹配一个原始的 \ 字符

对与 kotlin 来说,要写成 Regex("\\\\").matches(text)

所以对与 gkd 来说,要写成 [text~="\\\\\"]

对与 JavaScript/JSON/JSON5 来说,要写成 [text~="\\\\\\\\"]

matches : '[text~="\\\\\\\\"]'

如果你的规则文件是由 https://github.com/gkd-kit/subscription-template 构建

你可以使用 JavaScript 的原始字符串语法去除一次转义,这样只需要写一次转义

matches : String.raw`[text~="\\\\"]`

解决了,感谢!

@lisonge
Copy link
Member

lisonge commented Apr 19, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants