正则表达式匹配捕获组中的文本 [英] Regex Match text within a Capture Group
本文介绍了正则表达式匹配捕获组中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例文本:
\- !ruby/object:DynamicAttribute
attributes:
resource_id: "1"
resource_type: Applicant
string_value: "Michael"
int_value:
id: "35972390"
date_value:
name: first_name
attributes_cache: {}
\- !ruby/object:DynamicAttribute
attributes:
resource_id: "1"
resource_type: Applicant
string_value: "Johnson"
int_value:
id: "35533149"
date_value:
name: last_name
attributes_cache: {}
目标:
我试图在string_value"之后提取值,其中name"等于某个字符串.假设它等于 last_name.属性没有任何特定的顺序.我已经使用捕获组进行了探索,但并没有走多远.
I'm trying to extract the value after "string_value" where the "name" equals some string. Let's say it equals last_name. The attributes are not in any particular order. I've explored using capture groups but I did not get very far.
对此的任何帮助将不胜感激.谢谢!
Any help on this would be appreciated. Thanks!
推荐答案
你可以试试这个正则表达式:
You can try this regex:
string_value:(?=(?:(?!attributes_cache).)*name: last_name)\s+\"(\w+)\".*?attributes_cache
string_value:
匹配字符string_value:
- Positive Lookahead
(?=(?:(?!attributes_cache).)*name: last_name)
它会提前查看是否包含name: last_name
但会不要超出 attributes_cache ,否则它可能与下一个可能具有 name: last_name 的结果集重叠 \s+
匹配任何空白字符(等于 [\r\n\t\f\v ])- 量词 - 在一次和无限次之间匹配,尽可能多次,根据需要回馈(贪婪)
\"
字面上匹配字符"
(区分大小写)- 第一个捕获组
(\w+)
: \w+匹配任何单词字符(等于 [a-zA-Z0-9_])=> 这是您要捕获的文本.
string_value:
matches the charactersstring_value:
- Positive Lookahead
(?=(?:(?!attributes_cache).)*name: last_name)
it looks ahead to see if it containsname: last_name
but will not go beyond attributes_cache , otherwise it may overlap with the next result set which may have name: last_name \s+
matches any whitespace character (equal to [\r\n\t\f\v ])- Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\"
matches the character"
literally (case sensitive)- 1st Capturing Group
(\w+)
: \w+ matches any word character (equal to [a-zA-Z0-9_]) => this is the text that you want capture.
捕获组 1 包含您要查找的文本.
The capture group 1 contains the text that you are looking for.
虽然你没有描述编程语言但是下面的例子是在ruby上完成的(运行它):
Although you haven't described the programming language but the following sample is done on ruby (run it) :
re = /string_value:(?=(?:(?!attributes_cache).)*name: last_name)\s+\"(\w+)\".*?attributes_cache/m
str = '\\- !ruby/object:DynamicAttribute
attributes:
resource_id: "1"
resource_type: Applicant
string_value: "Johnson1"
int_value:
id: "35533149"
date_value:
name: last_name
attributes_cache: {}
\\- !ruby/object:DynamicAttribute
attributes:
resource_id: "1"
resource_type: Applicant
string_value: "Michael"
int_value:
id: "35972390"
date_value:
name: first_name
attributes_cache: {}
\\- !ruby/object:DynamicAttribute
attributes:
resource_id: "1"
resource_type: Applicant
string_value: "Johnson2"
int_value:
id: "35533149"
date_value:
name: last_name
attributes_cache: {}'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
这篇关于正则表达式匹配捕获组中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文