1

对于所有示例都需要“编程”作为输出

示例 1:这是一个编程测试。
正则表达式 : a+\sprogramming -- 工作

示例 2:这是一个测试编程。这是一个编程测试正则
表达式:a+\s(?!test)\sprogramming -- 工作

示例 3:这是一个测试编程。这是一个基本的编程测试
REGEX:a+\s(?!test)(.+)\sprogramming -- 不工作

实际 O/P : 一个基本的编程
预期的 O/P : 一个编程

也就是说,只需要打印一次'a'。

提前致谢。

4

2 回答 2

2

这是你想要的?

REGEX : .*(a programming).*

预期的 O/p:

1.9.3p194 :001 > "This is a test programming. This is a basic a programming test ".match(/.*(a programming).*/)
 => #<MatchData "This is a test programming. This is a basic a programming test " 1:"a programming"> 
1.9.3p194 :002 > "This is a programming test. ".match(/.*(a programming).*/) => #<MatchData "This is a programming test. " 1:"a programming"> 
1.9.3p194 :003 > "This is a test programming. This is a programming test ".match(/.*(a programming).*/)
 => #<MatchData "This is a test programming. This is a programming test " 1:"a programming"> 
于 2012-10-30T06:35:46.090 回答
0

I'm not sure I understand your question.

If you're looking to match a word between two words, such as "a" and "test", then you do this:

a\s+(\S+)\s+test

(The \S with a capital "S" matches non-whitespace characters.)

If you want only the last such instance, then:

a\s+(\S+)\s+test(?!.*a\s+(\S+)\s+test)

(Notice what's inside the negative lookahead assertion is a copy of the part outside.)

In addition, you would want to use word boundaries, e.g.

\ba\s+(\S+)\s+test\b(?!.*\ba\s+(\S+)\s+test\b)
于 2012-10-30T06:37:46.813 回答