在一个字符串后面匹配和空格?

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

Match after a string and whitespaces?

问题

I can provide the translated code portion without any additional content:

import re

proc extractColor(input: string, key: string): string =
  var regex = compileRegex(key & "=\s*([^\n\r]+)")
  if regex.search(input):
    return regex.match(input).group(1)
  else:
    return ""

let input = """
COLOR1=               Light Blue
COLOR2=    Dark Red
"""

let color1 = extractColor(input, "COLOR1")
let color2 = extractColor(input, "COLOR2")

echo color1
echo color2

This Nim code defines a extractColor procedure that extracts the color value associated with a given key from the input string while removing any leading whitespace. The regular expression used is compatible with the PCRE (Perl-Compatible Regular Expressions) library, which you mentioned is used in the Nim programming language.

英文:

If I have the following line, for example:

COLOR1=               Light Blue
COLOR2=    Dark Red

I'm looking for a regex that if I pass the string "COLOR1=" to it, it will match only "Light Blue", without all the whitespace before "Light". The same if I pass "COLOR2=", which should match only "Dark Red".

I tried (?<=COLOR1=)[^.]*, but it doesn't remove whitespace after the "=" and before the first non-empty character, and has problems with newlines.

EDIT: I am using the Nim programming language, which compiles to C code. Regular expressions in this language are based on PCRE (Perl-Compatible Regular Expressions) C library.

答案1

得分: 1

你可以使用 \K 来在匹配关键字、等号和后续空白字符之后重置匹配:

COLOR1=\s*\K.*

演示:https://regex101.com/r/zSwjKO/1

英文:

You can use \K to reset the match after matching the key, the equal sign and any subsequent whitespaces:

COLOR1=\s*\K.*

Demo: https://regex101.com/r/zSwjKO/1

答案2

得分: 1

尝试匹配正则表达式

(?m)^COLOR2=\s*(.+)

如果有匹配,捕获组1将包含所需的字符串。

示例

该表达式包含以下元素。

(?m)     # 设置多行标志
^        # 匹配行的开头
COLOR2=  # 匹配字面文本
\s*      # 匹配零个或多个空白字符
(        # 开始捕获组1
  .+     # 匹配除换行符之外的一个或多个字符
)        # 结束捕获组1

多行标志 m(在 (?m) 中)使 ^ 匹配行的开头和结尾。在某些编程语言中,多行标志的设置方式可能不同。在Ruby中,不需要该标志,因为 ^ 匹配行的开头(而 \A 匹配字符串的开头)。

英文:

Attempt to match the regular expression

(?m)^COLOR2=\s*(.+)

If there is a match capture group 1 will hold the desired string.

Demo

The expression has the following elements.

(?m)     # set the multiline flag
^        # match the beginning of a line
COLOR2=  # match literal
\s*      # match zero or more whitespace chars
(        # begin capture group 1
  .+     # match one or more chars other than line terminators 
)        # end capture group 1

The multiline flag m (in (?m)) causes ^ to match the start and end of a line. In some languages the multiline flag is set differently. In Ruby there is no need for that flag as ^ matches the beginning of a line (and \A matches the beginning of the string).

huangapple
  • 本文由 发表于 2023年2月24日 12:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552657.html
匿名

发表评论

匿名网友

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

确定