I want to scan a text composed of lines. When I recognize a keyword I am using a regex for this type of line.

My text has this form:

text<eol>
...
Function1 parameter1 value1, parameter2 value2, .... parameterN value N<eol>
...
text<eol>

In this case, when I recognize "Function1", I use this regex:

(?:(\w+)\s|\G(?<!^))[ ]*(\S+[ ]+\S+)\s*,?

$1 = the keyword Function1
$3 = parameterx valuex

I would like to stop the matching when the end of line is found.

while mytext =~ /.../ do
 case $3
  when 'parameter1 value1'
   ...
 end #case
 $'
end #while

My code is correct excepted when the matching is close to the end of line:my regex captures a part a the next line.

The question is: what can I add in my regex to stop when end of line ? I suppose I must add $ somewhere ?

Note: Sorry I copied the wrong regex ([] gave me an error with Ruby 2.0). Note the space before the first star !

(?:(\w+)(\s+)|\G(?<!^)) *(\S+\s+\S+)\s*,?
有帮助吗?

解决方案

Okay, since you asked for it, here it is:

(?:(\w+) +|(?!^)\G) *(\S+ +[^\n, ]+) *,?

I changed all the \s into whitespaces and the last \S to [^\n, ] so that the comma after the value is not consumed during the matching.

I made some more minor changes regarding the capture groups and the way you were negating the ^ for the \G anchor.

It's working on PCRE flavoured regex like on this demo site but like I said, I'm not entirely sure \G works on Ruby. Would be glad if someone could confirm this.

其他提示

To stop matching at the end of lines, you should first make sure that the dotall flag is not active.

If the regular expression

(?:(\w+)\s|\G(?<!^))[ ]*(\S+[ ]+\S+)\s*,?

is working for you, then simply add start (^) and/or end-of-line ($) boundaries to it, such as:

^(?:(\w+)\s|\G(?<!^))[ ]*(\S+[ ]+\S+)\s*,?$

But it seems that your goal is to first find a line with a function-signature and capture the function name. So first you'll need to match the entire line with

^(\w+)\s+(?:(?:\w+\s+\w+)(?:, *)?)+$

Regular expression visualization

Debuggex Demo

(The function name is in capture group one.)

Then, starting at the first non-whitespace character following the function name, iterate through each parameter with

\b(\w+)\s+(\w+)\b

Regular expression visualization

Debuggex Demo

Not sure about regex specific to ruby. But I think, adding [^<eol>] should help exclude new lines. Try this simple change:

(?:(\w+)(\s+)|\G(?<!^)) *(\S+\s+\S+)\s*[^<eol>],?
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top