Exclusive Regexps

Share your advanced PureBasic knowledge/code with the community.
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Exclusive Regexps

Post by Piero »

Using GNU versions of awk, sed and grep (on Mac you can install them via HomeBrew):

Code: Select all

echo "foobar" | gawk '{match($0,/^(.)(.+)(.)$/,a);print a[1],a[3]}'
echo "foobar" | gsed 's/^\(.\)\(.\+\)\(.\)$/\1 \3/'
echo "foobar" | ggrep -Poi '[^;]*procedure(?!return)[.]?\w*\s+\K.*\)' || echo "Not Found"
PB Named Group:

Code: Select all

; Find Comments to translate (";" must be outside strings)

; (\\"|"(?:\\"|[^"])*"|(\+))|;[\s]*(?<pbcomment>.+)
#_PBCOMMENT = ~"(\\\\\"|\"(?:\\\\\"|[^\"])*\"|(\\+))|;[\\s]*(?<pbcomment>.+)"
#_PBCN = 0 : CreateRegularExpression(#_PBCN, #_PBCOMMENT)

pbsource$= ~"line1 \"\\\"AZJ;IO\" ; tet;яis ;я яя \n"+
~"line2 \"я\" ;я\n"+
~";      line3 я\n"+
~"line4 ; pp"

nl = CountString(pbsource$, ~"\n") + 1
For k = 1 To nl
   pbsourceline$ = StringField(pbsource$, k, ~"\n") ; ReadString(#File [, Flags [, Length]])
   
   If ExamineRegularExpression(#_PBCN, pbsourceline$)
      While NextRegularExpressionMatch(#_PBCN)
         pbcm_match$ = RegularExpressionNamedGroup(#_PBCN, "pbcomment") ; <<<=== RTrim?
         if pbcm_match$ ; handle strange unicode lookahead "empty match" cases………
            Debug "Code Line: '" + pbsourceline$ + "'"
            if CountString(pbsourceline$, pbcm_match$) > 1
               Debug ~"****** SOMETHING IS WRONG HERE ******\n" ; multiple replacestring
            Else
               Debug "PureCode: '" + ReplaceString(pbsourceline$, pbcm_match$, "") + "'"
               Debug "Comment: '" + pbcm_match$ + ~"'\n"
            EndIf
         EndIf
      Wend
   EndIf
   
Next