sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    RegEx fun.... not.... :(

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 4 Posters 346 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • thomthomT Offline
      thomthom
      last edited by

      /^\*\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 matches result[1] if it matches the second it matches result[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?

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        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)

        Hi

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          @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/.

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            @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
            

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              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.

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                Wouldn't this work ?

                if text=~/^\*[0-9]/ || text=~/[0-9]\*$/
                  number=text.gsub(/[^0-9\.]/,'').to_i
                end
                

                TIG

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  @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
                  
                  

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Buy SketchPlus
                  Buy SUbD
                  Buy WrapR
                  Buy eBook
                  Buy Modelur
                  Buy Vertex Tools
                  Buy SketchCuisine
                  Buy FormFonts

                  Advertisement