RegEx fun.... not.... :(
-
/^\*\s*(\d+)/
This will capture a string like "15" or " 15" and return the result "15".` result = text.match(/^*\s*(\d+)/)
result[1]15`
/^(\d+)\s*\*/
This will capture a string like "15*" or "15 *" and return the result "15".
So far so fine.But I tried to make a regex look for both patterns:
/^(?:\*\s*(\d+))|(?:(\d+)\s*\*)/
Which it does - but I expected the results to be located in
result[1]
- but it's not.
If it matches the first pattern it matchesresult[1]
if it matches the second it matchesresult[2]
.
I'd thought that since I grouped the two patterns and separated with a pipe it would be the same as/(foo|bar)/
.Maybe it's just too early in the day - and week - for thinking regex, but can anyone swat me over the head with some good sense to what I should be doing?
-
Thom,
Is there more to the String, or is it simply a digit prefixed or suffixed with a *?
If the String is simple, maybe just check if a * exists anywhere, and pick just the number using a Regex.
has_star = (str.index('*') != nil)
-
@jim said:
Thom,
Is there more to the String, or is it simply a digit prefixed or suffixed with a *?
If the String is simple, maybe just check if a * exists anywhere, and pick just the number using a Regex.
It's for Edge Tool's Divide Face feature where you use the VCB to multiply and divide by typing 5, 5, /5 or 5/.
-
@thomthom said:
I'd thought that since I grouped the two patterns and separated with a pipe it would be the same as
/(foo|bar)/
.'laladededfoobar' =~ /(foo|bar)/
9
returns the index at which the match occurs, or nil if no match.'laladededfoobar'.match( /(foo|bar)/ )
actually, is converted into:
/(foo|bar)/.match( 'laladededfoobar' )
see: String#match and Regexp#match in the Ruby reference,
which returns nil if no match, or a MatchData object if there is a match.so:
# store the match and test for nil in 1 go if m = /(foo|bar)/.match( 'laladededfoobar' ) # there was indeed a match, so the Ruby pattern matching # globals $&, $+, $=, $`, $', $1..$9 and $~ come into play # and are local to the current scope !! num = $+ else # there was no match end
Back to your specific example:
# define a Regexp rx = /(\*\s*(\d+)|(\d+)\s*\*)/ # begin a loop # set txt to some substring of the VCB text if num=( rx.match(txt) ? $+ ; nil ) # use num else # recover end # repeat until no more VCB text to parse # end loop
-
So it's not possible to do what I intended with regex?
This works:
<span class="syntaxdefault"><br />if result </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> text</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">match</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/^[*</span><span class="syntaxdefault">x</span><span class="syntaxkeyword">]</span><span class="syntaxdefault">s</span><span class="syntaxkeyword">*(</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">+)|(</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">+)</span><span class="syntaxdefault">s</span><span class="syntaxkeyword">*[*</span><span class="syntaxdefault">x</span><span class="syntaxkeyword">]/</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> number </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> result</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">compact</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">to_i<br />end<br /></span>
But I just wanted to understand regex better - to why I could not do what I tried to do.
-
Wouldn't this work ?
if text=~/^\*[0-9]/ || text=~/[0-9]\*$/ number=text.gsub(/[^0-9\.]/,'').to_i end
-
@thomthom said:
But I just wanted to understand Regexp better - to why I could not do what I tried to do.
I thot I explained it. Your first example, returns the position of a match, IF it occurs using =~ /(foo|bar)/ ... which is an integer index.
In contrast, both match() methods return a MatchData object.@thomthom said:
So it's not possible to do what I intended with Regexp?
... BUT you then went on to say what you wished was to have the matched numerical string returned, so let me simplify the code (the secret is the $+ global pattern matching variable):
# define a Regexp rx = /(\*\s*(d+)|(d+)\s*\*)/ rx.match('* 15') $+.to_i >> 15 rx.match('15 *') $+.to_i >> 15
Advertisement