如何使我的正则表达式匹配单行和多行括号之间的变量?

huangapple go评论69阅读模式
英文:

How can I make my regular expression match variables inbetween brackets on single and multiple lines?

问题

我试图匹配从 process.env 中解构的所有环境变量,在我的 TypeScript 项目中。这包括在单行或多行上进行解构。

以下是包含从 process.env 解构的 TS 代码示例,一个在单行上,另一个跨多行:

File1.ts:

const { OKTA_AUDIENCE_DOMAIN, OKTA_DOMAIN = '''', OKTA_WEB_CLIENT_ID, OKTA_ISSUER: issuer = '''', AUTH_TWO: ISSUE_TWO = '''', } = process.env;

File2.ts:

const {
  OKTA_AUDIENCE_DOMAIN,
  OKTA_DOMAIN = '''',
  OKTA_WEB_CLIENT_ID,
  OKTA_ISSUER: issuer = '''',
  AUTH_TWO: ISSUE_TWO = '''',
} = process.env;

我只能编写一个匹配单行解构变量的脚本。但是,我希望这个脚本可以匹配单行和多行。

这是我目前拥有的脚本:

grep -Ezo '\{[^}]*\} = process.env' File1.ts

如果在 File1 上运行,我将得到以下输出,这是我期望的:

{ AUTH0_AUDIENCE_DOMAIN, AUTH0_DOMAIN = '''', AUTH0_WEB_CLIENT_ID, AUTH0_ISSUER: issuer = '''', AUTH_TWO: ISSUE_TWO = '''', } = process.env

你可以看到这里已经正确地匹配了从 process.env 解构的环境变量。

现在,如果我在 File2.ts 上运行相同的脚本,它将返回空:

grep -Ezo '\{[^}]*\} = process.env' File2.ts

输出为空。

我应该如何修改这个脚本,以便它可以匹配在单行和多行上进行解构的环境变量?

英文:

I am trying to match all environment variables that get de-structured from process.env in my Typescript project. This includes matching de-structuring on single or multiple lines.

Take the following TS code examples that contains de-structuring from process.env, 1 is on a single line, the other is across multiple lines:

File1.ts:

const { OKTA_AUDIENCE_DOMAIN, OKTA_DOMAIN = '', OKTA_WEB_CLIENT_ID, OKTA_ISSUER: issuer = '', AUTH_TWO: ISSUE_TWO = '', } = process.env;

File2.ts:

const {
  OKTA_AUDIENCE_DOMAIN,
  OKTA_DOMAIN = '',
  OKTA_WEB_CLIENT_ID,
  OKTA_ISSUER: issuer = '',
  AUTH_TWO: ISSUE_TWO = '',
} = process.env;

I have only been able to write a script that matches the de-strucutred variables on the single line. However, I want the script to match single and multi lines.

This is the script I currently have:

grep -Ezo '\{[^}]*\} = process.env' File1.ts

If ran on File1, I would get the following output, which is what I expect:

{ AUTH0_AUDIENCE_DOMAIN, AUTH0_DOMAIN = '', AUTH0_WEB_CLIENT_ID, AUTH0_ISSUER: issuer = '', AUTH_TWO: ISSUE_TWO = '', } = process.env

You can see here that it has correctly matched the environment variables being de-structured from process.env

Now if I were to run this same script on File2.ts, it would return nothing:

grep -Ezo '\{[^}]*\} = process.env' File2.ts

The output is empty.

How can I modify this script such that it matches the environment variables being de-structured on both single and multiple lines?

答案1

得分: 1

尝试这个:

cat File1.ts | tr '\n' ' ' | sed 's/ / /g' | grep -Ezo '{[^}]} = process.env'
cat File2.ts | tr '\n' ' ' | sed 's/ / /g' | grep -Ezo '{[^}]} = process.env'


注意:
- `tr`将换行符更改为空格(如果你在Windows上,将其更改为 `tr '\n\r' '  '`)
- `sed`去除多个空格
- `grep`保留了你的原始参数
英文:

Try this:

cat File1.ts | tr '\n' ' ' | sed 's/  */ /g' | grep -Ezo '\{[^}]*\} = process.env'
cat File2.ts | tr '\n' ' ' | sed 's/  */ /g' | grep -Ezo '\{[^}]*\} = process.env'

Notes:

  • tr changes newlines to spaces (change to tr '\n\r' ' ' if you are on Windows)
  • sed gets rid of multiple spaces
  • grep has your original parameters

答案2

得分: 0

如果您同意安装ripgrep

rg -oU '\{[^}]*\} = process\.env'

-U选项启用多行匹配。

话虽如此,对于您提供的示例输入,grep -oz '{[^}]*} = process\.env' 对我来说有效。另外,请注意,使用grep -z输出将以ASCII NUL字符终止。因此,您需要进一步处理输出,例如通过管道传递给tr '\0' '\n'

英文:

If you are okay with installing ripgrep

rg -oU '\{[^}]*\} = process\.env'

The -U option enables multiline matching.

That said, grep -oz '{[^}]*} = process\.env' works for me for your given input samples. Also, note that with grep -z the output will be terminated with ASCII NUL characters. So, you'll need to further process the output, for example pipe to tr '\0' '\n'

huangapple
  • 本文由 发表于 2023年2月6日 13:54:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定