Announcement

Collapse
No announcement yet.

RegEx Question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • RegEx Question

    Can someone help me with a simple RegEx. I am looking to return each $Command(Parameter).

    According to RegExBuddy I could just use \$\w+\(\w*\) but that's not valid PB RegEx syntax so I expanded (and simplified) the \w token.

    Code:
     
    FUNCTION PBMAIN( )
     LOCAL lngLen AS LONG
     LOCAL lngPos AS LONG
     LOCAL strRegEx AS STRING
     LOCAL strTestString AS STRING
     strRegEx = "\$[a-z]+\([a-z]*\)"
     strTestString = "/Switch1=$Command1(Parameter1) /Switch2=$Command2(Parameter2)"
     REGEXPR strRegEx IN strTestString TO lngPos, lngLen
     MSGBOX _
     "Position = " & FORMAT$( lngPos ) & $CRLF & _
     "Length   = " & FORMAT$( lngLen ) & $CRLF & _
     "Text     = " & MID$( strTestString, lngPos, lngLen )
    END FUNCTION
    Last edited by George Bleck; 3 Jul 2009, 10:01 AM.
    <b>George W. Bleck</b>
    <img src='http://www.blecktech.com/myemail.gif'>

  • #2
    If you are looking for the dollar sign '$' and/or parens '()' you will have to escape them since they are both metacharacters in PB regular expressions

    You can try something like
    Code:
     StrRegEx =    "\S"   _           ' shortest (else you could get the whole line)
                    & "/Switch"      _    ' literal
                    &  "0-9"          _   ' exactly one numeric digit 
                    &  "="            _   '  literal  
                    & "\x24"         _   ' dollar sign in accepted format
                    & "Command"  _   '  literal
                    & "0-9"          _   '  exactly one numeric digit
                    & "\x28"         _    ' left paren
                    & "(.+)"            _    ' any characters, tagged (ranges may not be tags)
                   & "\x29"          _   ' rightParem
                   & "\w"                 ' word break 
    
      ReplaceMask  =  "\1"       ' first tagged group
    
     REGREPL strRegEx IN strTestString with ReplaceMask TO  iPos, paramvalue
    .. and maybe do it in a loop as shown in this demo...

    REGEXPR and REGREPL demo January 16, 2002

    The above is probably not exactly what you want but I think by escaping the metatcharacters you will be off to a good start.

    (that demo has multiple examples in it, too).

    Didn't someone post a nifty "regular expression" tester program here once? Where you can just type in your strings and click a button and you get the answer?

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Mike, thanks for your assistance. Unfortunately you were a bit TOO literal, I guess as hard as I tried to be descriptive I still failed.

      The patterns I am looking to enumerate must:

      (1) Start with a "$"
      (2) Have one or more letters/numbers following the "$"
      (3) Have a "(" following all the previous characters
      (4) Have any combination of letters/numbers (or nothing) following the "("
      (5) End with a ")"
      Examples
      $Command(Parameter) is valid
      $Word(MoreWords) is valid
      $(SomethingSpecial) is NOT valid
      $BlankParameter() is valid
      MissingSomething(SomeText) is NOT valid
      I am not worried with the looping to search for them, that much I can handle. I am just having a hard time nailing the basic RegEx. It works perfect in RegExBuddy but I just can't get it to work in PB.
      <b>George W. Bleck</b>
      <img src='http://www.blecktech.com/myemail.gif'>

      Comment


      • #4
        OK, Never mind... big DUH on my part...I forgot to account for numbers which is why my original example failed, even after I escaped out the "$", "(", and ")" characters... took me awhile to catch that stupid error. Also the "\s" helps.

        Below winds up being the "final" code. I changed the rules a bit and forced the parameter to be inbetween quotes and parenthesis as it looks cleaner in my production work.

        Code:
         
         
        '----------------------------------------------------------------------------(')
         
        #INCLUDE "win32api.inc"
         
        '----------------------------------------------------------------------------(')
         
        FUNCTION GetCommands( strSearchBuffer AS STRING, strArray( ) AS STRING ) AS LONG
         LOCAL lngMatchCount AS LONG
         LOCAL lngMatchLength AS LONG
         LOCAL lngMatchPosition AS LONG
         LOCAL lngSearchPosition AS LONG
         LOCAL strRegEx AS STRING
         ERASE strArray( )
         ' Enumerate the following mask into an array:  $CommandText("Parameter")
         strRegEx = "\s\x24[a-z0-9]+\x28\q(.*)\q\x29"
         DO
          REGEXPR strRegEx IN strSearchBuffer AT lngSearchPosition TO lngMatchPosition, lngMatchLength
          IF lngMatchPosition THEN
           ' We have a match!
           ' Increment the match count, resize the array accordingly,
           ' then save the match to the new last slot in the array
           INCR lngMatchCount
           REDIM PRESERVE strArray( lngMatchCount - 1 )
           strArray( lngMatchCount - 1 ) = MID$( strSearchBuffer, lngMatchPosition, lngMatchLength )
           ' Change the search position and keep trying
           lngSearchPosition = lngMatchPosition + lngMatchLength
           ITERATE
          END IF
          EXIT LOOP
         LOOP
         FUNCTION = lngMatchCount
        END FUNCTION
         
        '----------------------------------------------------------------------------(')
         
        FUNCTION PBMAIN( )
         DIM strArray( 0 ) AS STRING
         LOCAL lngArrayIndex AS LONG
         LOCAL lngMatchCount AS LONG
         LOCAL strTestString AS STRING
         strTestString = "C:\MyPath\MyEXE.exe " & _
           "/Switch1=Command1(""Parameter1"") " & _
           "/Switch2=$Command2("""") " & _
           "/Switch3=$Command3(""Parameter3"") " & _
           "/Switch4=$(""Parameter4"")"
         MSGBOX "Test string:" & $CRLF & $CRLF & strTestString
         lngMatchCount = GetCommands( strTestString, strArray( ))
         IF lngMatchCount THEN
          MSGBOX FORMAT$( lngMatchCount, "###  " ) & "Item(s) found"
          FOR lngArrayIndex = LBOUND( strArray ) TO UBOUND( strArray )
           MSGBOX FORMAT$( lngArrayIndex + 1, "###  " ) & strArray( lngArrayIndex )
          NEXT lngArrayIndex
         ELSE
          MSGBOX "No commands were present"
         END IF
        END FUNCTION
        Last edited by George Bleck; 3 Jul 2009, 09:12 PM.
        <b>George W. Bleck</b>
        <img src='http://www.blecktech.com/myemail.gif'>

        Comment


        • #5
          Didn't someone post a nifty "regular expression" tester program here once? Where you can just type in your strings and click a button and you get the answer?
          Um, yes, someone did.

          This was posted by Kevin Voell 6/11/06:

          http://www.powerbasic.com/support/pb...ad.php?t=24927

          (That's for PB/Win. )
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Originally posted by Michael Mattias View Post
            This was posted by Kevin Voell 6/11/06:

            http://www.powerbasic.com/support/pb...ad.php?t=24927

            (That's for PB/Win. )
            I just grabbed that to try it out and I just wanted to point out that the source seems to have been pasted into the message twice so you need to remove the second copy before it will compile. PB/WIN v9.01 complained when it hit the second #COMPILE EXE statement.
            Jeff Blakeney

            Comment


            • #7
              Obviously this is an old post, but I was looking for stuff on PB's Regex and found this post, so figured other people might also find PB's online Regex Tester useful. There is a clickable link in the Helpfile (at least in PBCC6 there is anyway), but I got the actual link so it can be accessed via your browser too. I'm not sure if the source code for that is around anywhere, perhaps somebody else knows?

              http://www.powerbasic.com/bin/regexpr.exe?from=pbcc

              Comment


              • #8
                PB/Win (and maybe PB/CC) help used to have a link to an online REGEXPR tester somewhere at powerbasic.com.

                (Under, duh, REGEXPR). May still be there.

                [ADDED]
                I couldn't resist. PB/Win 10 help does have a link to it. It's under "See also" near the bottom of the REGEXPR page. The tester page even has a little "control character help" right on the screen.

                There is no such link under REGREPL.
                Last edited by Michael Mattias; 14 Nov 2014, 09:27 AM.
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  By the way, I have been wondering what 'flavour' (or flavor for you Yanks) of Regex is the PB Regex? For example, RegexMagic and RegexBuddy can handle Perl, C++, C#, Java, etc, 194 in total at present. I'm not sure if PB fits close enough to any of those 194, but if so, perhaps JGSoft (who wrote those two programs) might be willing to add PB to the mix. I found RegexMagic really useful for some stuff I was doing a while back, so it would be nice to have it suitable for PB. Also, that link I posted above is now no longer so perhaps the moderators / admin can fix it?

                  Comment


                  • #10
                    I have already reported the missing Webpage. FWIW , I created my own locally program now for quick testing, nothing more than a wrapper.



                    A dozen what.

                    Comment


                    • #11
                      Thanks. That sounds interesting Michael. I haven't used the PB Regex as yet, though I do have a program where I've often thought it would be useful, if I ever get back to it!

                      Comment


                      • #12
                        By the way, I have been wondering what 'flavour' (or flavor for you Yanks) of Regex is the PB Regex?
                        That would be the 'PowerBASIC' flavor. No, it's not Perl, C++, C# or Java as you mention; and for good measure it's also neither Unix nor Ultra-Edit.
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          OK, so it's unique.I believe the Ultra-Edit Regex was very limited, but the now can run Perl which I'm told is far far better. I might ask JG soft if they'd consider adding a PB flavour, though I expect it's probably too small a user-base for them to be interested, relative to the C++, etc user-base. I guess a large part would depend on just how different it was.

                          Comment


                          • #14
                            >I might ask JG soft if they'd consider adding a PB flavour,

                            Maybe not. IMO the PB REGEXPR and REGREPL statements are not as feature-rich as are the other flavors.
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                            • #15
                              Originally posted by Michael Mattias View Post
                              >I might ask JG soft if they'd consider adding a PB flavour,

                              Maybe not. IMO the PB REGEXPR and REGREPL statements are not as feature-rich as are the other flavors.
                              They play well with other string functions in PB , somewhat levels the playing field so I think, like mid$

                              Down to brass tacks

                              While pb don't have bla you may always improvise, a tool to create or convert would be nice. Here is a simple example.


                              For Example - some have numeric repeat for part of the mask expression , easy to replicate in PB.

                              Off the top of my head lets say {(z)'x5)'} or some notation like this would equal (zzzzz) 5 z's for exact match

                              pb use "(" + REPEAT$(5 "z") + ")" or (zzzzz)



                              a not so easy, because group can be changed during runtime in a script by the user.

                              define {GROUP} ="ABCDEFG"

                              {GROUP} - 'G'

                              group now equals ABCDEF

                              or {GROUP} + '1'

                              now ABCDEF1


                              lot more work in pb , several ways to approach this. Regex might be off the table , it would be nice if a tool could convert it to PB


                              I am sure you get the point

                              Mike

                              added: is this worth 30-50 dollars for a user ?

                              A dozen what.

                              Comment


                              • #16
                                The PB REGEXPR can get a bit unwieldy what with its "nonsupport" for the "repeat" option available with other flavors. Real Life example:
                                Code:
                                ; Setting two Alpha, 3-5 numeric, 'P', 2-5 numeric; suffix all alpha up to first numeric
                                FI,621611429,[B][a-z][a-z][0-9][0-9][0-9][0-9]?[0-9]?P[0-9][0-9][0-9]?[0-9]?[0-9]?[/B],[B][a-z]+[/B]
                                This example actually comes from an "INI" file in which the user would enter his own masks (there are two masks here: first is the two A,3-5 N, P, 2-5 N; second is all alpha).
                                If a user can do it, so can you. (Yes, I created this particular example for him).

                                But as far as " it would be nice if a tool could convert it to PB", I have made my opinion very clear on trying to do verb-for-verb, function-for-function conversions from one source code language to another language many times (=stone loser) and that view holds here.




                                Michael Mattias
                                Tal Systems (retired)
                                Port Washington WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment

                                Working...
                                X