Reputation: 2918
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*,?
Upvotes: 1
Views: 7955
Reputation: 20163
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+)(?:, *)?)+$
(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
Upvotes: 1
Reputation: 227
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>],?
Upvotes: 0
Reputation: 71538
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.
Upvotes: 1