Regex 用于[ABC]、ABC和ABCxx等模式的正则表达式,其中xx是一个数字
我有一个文本的长度可以在1到1000之间变化。我希望从文本中提取以下子字符串Regex 用于[ABC]、ABC和ABCxx等模式的正则表达式,其中xx是一个数字,regex,ruby,Regex,Ruby,我有一个文本的长度可以在1到1000之间变化。我希望从文本中提取以下子字符串 ABCxx/ABCx形式的子字符串,其中ABC始终是英文字母,x/xx是一个从0到99不等的数字(数字长度为1或2)。下面的正则表达式为我解压这个子字符串-[a-zA-Z]{3}[0-9]{1,2} 格式为ABC、ABC(文本中最后一个子字符串/单词)和ABC(文本中第一个子字符串)的子字符串。基本上,我在这里试图找到一个由3个字母组成的单词,并在文本中用空格分隔。 为了获得上述匹配,我有以下正则表达式 [[a-zA-
[a-zA-Z]{3}[0-9]{1,2}
为了获得上述匹配,我有以下正则表达式
[[a-zA-Z]{3}[],[[a-zA-Z]{3}和[a-zA-Z]{3}[]
\[([a-zA-Z]{3})\]
由于模式或多或少相似,是否有必要将所有5种模式组合在一起
例如:ABC猫垫狗[老鼠]LAN45吃HGF1 jkhgkj ABC
这里有效的匹配项是ABC
,rat
,LAN45
,eat
,HGF1
,ABC
R = /
\p{L}{3}\d{1,2} # match 3 letters followed by 1 or 2 digits
| # or
(?<=\A|\p{Space}) # match start of string or a space in a pos lookbehind
(?: # begin a non-capture group
\p{L}{3} # match three letters
| # or
\[\p{L}{3}\] # match three letters surrounded by brackets
) # end of non-capture group
(?=\p{Space}|\z) # match space or end of string in a pos lookahead
/x # free-spacing regex definition mode
此正则表达式按常规编写(非自由间距模式):
我相信这与问题的陈述是一致的,但如果“BCD12”
前面有字母或后面有数字(这里两者都适用),则应按如下方式修改正则表达式
R = /
(?<=\A|\p{Space}) # match start of string or a space in a pos lookbehind
(?: # begin a non-capture group
\p{L}{3} # match three letters
\d{,2} # match 0, 1 or 2 digits
| # or
\[\p{L}{3}\] # match three letters surrounded by brackets
) # end of non-capture group
(?=\p{Space}|\z) # match space or end of string in a pos lookahead
/x # free-spacing regex definition mode
"ABC catmat dogdog [rat] XLAN45 eat HGF123 jkhgkj abc".scan R
#=> ["ABC", "[rat]", "eat", "abc"]
R=/
(?[“ABC”,“老鼠],“吃”,“ABC”]
请注意,在这两个正则表达式中,我都用空格字符替换了\p{Space}
。在自由空格模式下,在解析正则表达式之前,空格会被删除,因此它们必须写入\p{Space}
,[[:Space:]
,[]
(包含空格的字符类),\
一个转义空格字符,或者,如果合适的话,\s
一个空白字符(包括空格、换行符、制表符和一些其他字符)
此正则表达式按常规编写(非自由间距模式):
我相信这与问题的陈述是一致的,但如果“BCD12”
前面有字母或后面有数字(这里两者都适用),则应按如下方式修改正则表达式
R = /
(?<=\A|\p{Space}) # match start of string or a space in a pos lookbehind
(?: # begin a non-capture group
\p{L}{3} # match three letters
\d{,2} # match 0, 1 or 2 digits
| # or
\[\p{L}{3}\] # match three letters surrounded by brackets
) # end of non-capture group
(?=\p{Space}|\z) # match space or end of string in a pos lookahead
/x # free-spacing regex definition mode
"ABC catmat dogdog [rat] XLAN45 eat HGF123 jkhgkj abc".scan R
#=> ["ABC", "[rat]", "eat", "abc"]
R=/
(?[“ABC”,“老鼠],“吃”,“ABC”]
请注意,在这两个正则表达式中,我都用空格字符替换了
\p{Space}
。在自由空格模式下,在解析正则表达式之前,空格会被删除,因此它们必须写入\p{Space}
,[[:Space:]
,[]
(包含空格的字符类),\
一个转义空格字符,或者,如果合适的话,\s
一个空白字符(包括空格、换行符、制表符和一些其他字符)。谢谢大家的回答。这个正则表达式帮了我的忙
(\b[a-zA-Z]{3}([0-9]{1,2})?\b)谢谢大家的回答。这个正则表达式帮了我的忙
(\b[a-zA-Z]{3}([0-9]{1,2})?\b您能发布一些示例输入和所需输出吗?很抱歉,忘记了。刚刚编辑了一个示例。您可以在此处尝试类似于[abc]123的内容,abc是一个匹配项,因为它位于框括号内。如果字符串是“ABCD12”,则“BCD12”是否匹配?如果字符串是“BCD123”,则会匹配“BCD12”是否匹配?能否发布一些示例输入和所需的输出?抱歉,忘记了这一点。只需编辑问题以包含一个示例。您可以在此处尝试类似于[this]()的内容,“[abc]123”,abc是匹配的,因为它位于框括号内。如果字符串为“ABCD12”,则“BCD12”是否匹配?如果字符串为“BCD123”,则为“BCD12”“是否匹配?您问题中的示例建议
[rat]
应该匹配,但此正则表达式与rat
匹配。已编辑。我的错误。您问题中的示例建议[rat]
应该匹配,但此正则表达式与rat
匹配。已编辑。我的错误。”。
"ABCD123 [efg]456".scan R
#=> ["BCD12"]
R = /
(?<=\A|\p{Space}) # match start of string or a space in a pos lookbehind
(?: # begin a non-capture group
\p{L}{3} # match three letters
\d{,2} # match 0, 1 or 2 digits
| # or
\[\p{L}{3}\] # match three letters surrounded by brackets
) # end of non-capture group
(?=\p{Space}|\z) # match space or end of string in a pos lookahead
/x # free-spacing regex definition mode
"ABC catmat dogdog [rat] XLAN45 eat HGF123 jkhgkj abc".scan R
#=> ["ABC", "[rat]", "eat", "abc"]