#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Replacing words in string


    Hi all,
    I'm new to regex and got some help with this a couple of years ago, i'm now trying to get this to accept new terms.

    if you had the following:[code]Option Explicit
    Sub MyCode()
    'some running code
    End Sub

    Public Sub MyCode()
    'some running code
    End Sub

    Private Sub MyCode()
    'some running code
    End Sub

    Function MyFunc(FUNC as boolean)
    'Some running code
    End Function

    Public Function MyFunc(FUNC as boolean)
    'Some running code
    End Function

    Private Function MyFunc(FUNC as boolean)
    'Some running code
    End Function

    The result should be
    Code:
    Public Function MyFunc(FUNC as boolean)
    'Some running code
    End Function
    the result would be:
    Code:
    Option Explicit 
    Sub MyCode() 
         'some running code
    End Sub 
    
    Public Sub MyCode() 
         'some running code
    End Sub
    
    Private Sub MyCode() 
         'some running code
    End Sub
    Code:
    Function MyFunc(FUNC as boolean)
    'Some running code
    End Function
    
    Public Function MyFunc(FUNC as boolean)
    'Some running code
    End Function 
    
    Private Function MyFunc(FUNC as boolean)
    'Some running code
    End Function
    So as you can see the subs and functions have been split, the regex should look for Option Explicit and if it exists add [Kode] before it then find the last End Sub and add [/Kode] after it, if Option Explicit does not exist then put [Kode] before the first SUB, Public Sub or Private Sub then find the last End Sub and add [/Kode] after it.

    The same should work for the Functions, the regex below works however it is adding [Kode] before the option Explicit and again before the first SUB, Public Sub or Private Sub, can someone cast an eye over it and tell me what i've done wrong?


    PHP Code:
    $data preg_replace("/(?<!\[code\])\s*(Option Explicit\n+|Option Explicit\+|Private\s+|Public\s+)?(Sub|Function)(?!\s*\[\/code\])\s+(.*)End\s+\\2(?!\s*\[\/code\])/siU"'[CODE]\\1\\2 \\3 End \\2[/CODE]' "\n"$data); 
    I've changed code for Kode to stop this forum parsing the tags i needed to show you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    776
    Rep Power
    495
    I don't understand what you want to do. Your desired result looks the same as the input. What are you trying to do?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Laurent_R
    I don't understand what you want to do. Your desired result looks the same as the input. What are you trying to do?
    Not quite , the result as you can see is that all the functions are split from all the subs, also i have wrapped the source text and the result text in [Kode] tags, the purpose of this regex is to automatically do that too as people post code on my site without using the code tags, rather than beat them over the head to do it i'm using this regex to do it if these parameters are detected.

    Is that a little clearer? - it made sense in my head
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    225
    Rep Power
    41
    Using your data example:
    Code:
    Option Explicit 
    Sub MyCode() 
         'some running code
    End Sub 
    
    Public Sub MyCode() 
         'some running code
    End Sub
    
    Private Sub MyCode() 
         'some running code
    End Sub
    
    Function MyFunc(FUNC as boolean)
    'Some running code
    End Function
    
    Public Function MyFunc(FUNC as boolean)
    'Some running code
    End Function 
    
    Private Function MyFunc(FUNC as boolean)
    'Some running code
    End Function

    This worked for me:
    Code:
    $data = preg_replace('%(?<!\[code\])\s*(Option Explicit\n*|Option Explicit\s*|Private\s*|Public\s*)?(Sub|Function)(?!\s*\[/code\])\s+(.*)End\s*\2(?!\s*\[/code\])%si', '
    Code:
    \1\2 \3 End \2
    \n', $data);

    PHP Code:
    [CODE]Option Explicit 
    Sub MyCode
    () 
         
    'some running code
    End Sub 

    Public Sub MyCode() 
         '
    some running code
    End Sub

    Private Sub MyCode() 
         
    'some running code
     End Sub[/CODE]

    [CODE]Function MyFunc(FUNC as boolean)
    '
    Some running code
    End 
    Function

    Public Function 
    MyFunc(FUNC as boolean)
    'Some running code
    End Function 

    Private Function MyFunc(FUNC as boolean)
    '
    Some running code
     End 
    Function[/CODE
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Thanks for the reply, i dont understand it though, i thought using preg_replace we'd set the variable $data to be the expression and in that expression we'd have to use $data again for the string to look in like my old version which ends like this
    PHP Code:
    [KODE]\\1\\\\3 End \\2[/KODE]' . "\n", $data); 
    or has your solution not been copied over correctly?

    Like i said this is unkown territory to me
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    225
    Rep Power
    41
    Yeah, I didn't catch it, the previous post didn't have the complete ststement:
    PHP Code:
    $data preg_replace('%(?<!\[code\])\s*(Option Explicit\n*|Option Explicit\s*|Private\s*|Public\s*)?(Sub|Function)(?!\s*\[/code\])\s+(.*)End\s*\2(?!\s*\[/code\])%si''[CODE]\1\2 \3 End \2[/CODE]\n'$data); 

    Comments on this post

    • Simon Lloyd agrees : Great work!, thanks for the fix.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Great thanks!, it appears to work very well, i'll need to do some testing of different scenarios but it looks like you've fixed it for me.

    Most of the testing i have to do now is for if Option Explicit having a blank line before either public, private or sub and where functions are mixed inbetween subs rather than all subs at the top and all functions at the bottom like i had in my example.

    Once again thanks for fixing this for me
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Hi guys, i'm afraid i've come across a problem with the regex supplied, if the user posts

    Sub MyCode()
    'some running code
    End Sub

    Here is some extra text that they added in between

    Public Sub MyCode()
    'some running code
    End Sub


    The the result after the regex has taken care of it is
    [Kode][Kode][Kode]
    Sub MyCode()
    'some running code
    End Sub[/Kode]

    Here is some extra text that they added in between

    [Kode]Public Sub MyCode()
    'some running code
    End Sub[/Kode][/Kode][/Kode]

    Its adding an extra set of [Kode] tags around the entire lot just because of the extra line of text between the subs, i'm assuming this would happen for the functions too, can we get it to ONLY wrap the subs or functions (with prefixes or Option Explicit if they exist) and leave the text in between untouched?
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    225
    Rep Power
    41
    I don't understand, I use your example:
    Code:
    Option Explicit 
    Sub MyCode() 
         'some running code
    End Sub 
    
    Here is some extra text that they added in between
    
    Public Sub MyCode() 
         'some running code
    End Sub
    
    Private Sub MyCode() 
         'some running code
    End Sub
    
    Function MyFunc(FUNC as boolean)
    'Some running code
    End Function
    
    Public Function MyFunc(FUNC as boolean)
    'Some running code
    End Function 
    
    Private Function MyFunc(FUNC as boolean)
    'Some running code
    End Function
    And using the supplied regex I get:
    PHP Code:
    [CODE]Option Explicit 
    Sub MyCode
    () 
         
    'some running code
    End Sub 

    Here is some extra text that they added in between

    Public Sub MyCode() 
         '
    some running code
    End Sub

    Private Sub MyCode() 
         
    'some running code
     End Sub[/CODE]

    [CODE]Function MyFunc(FUNC as boolean)
    '
    Some running code
    End 
    Function

    Public Function 
    MyFunc(FUNC as boolean)
    'Some running code
    End Function 

    Private Function MyFunc(FUNC as boolean)
    '
    Some running code
     End 
    Function[/CODE
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Hi, thanks for the reply, the result should be
    PHP Code:
    [CODE]Option Explicit  
    Sub MyCode
    ()  
         
    'some running code 
    End Sub [/CODE] 

    [B]Here is some extra text that they added in between[/B] 

    [CODE]Public Sub MyCode()  
         '
    some running code 
    End Sub 

    Private Sub MyCode()  
         
    'some running code 
     End Sub[/CODE] 

    [CODE]Function MyFunc(FUNC as boolean) 
    '
    Some running code 
    End 
    Function 

    Public Function 
    MyFunc(FUNC as boolean
    'Some running code 
    End Function  

    Private Function MyFunc(FUNC as boolean) 
    '
    Some running code 
     End 
    Function[/CODE
    Where the bold section would not be in the tags. I dont understand why it added extra tags unless the user had added the tags and then the regex added themm too?
  20. #11
  21. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,920
    Rep Power
    1045
    Hi,

    "If all you have is a hammer, everything looks like a nail."

    Seriously, guys, grab/write a parser for that language. I understand that many people think regexes are a kind of all-powerful parsing tool (because that's all they know), but they are not. Regular grammars are the most primitive of all. They are perfect for simple tasks like validating a date, but they are not suitable for processing complex languages like this.

    While it is sometimes possible to use regexes as a quick hack and workaround, the result is just a bunch of ugly, unreadable and unmaintainable code. Good luck rewriting your regexes if there's a small change (or bug) somewhere.

    So if this is a serious project in any way and you care a tiny bit about good code, drop that regex stuff and do it right. Is this a known language? VBA maybe? In that case, there's almost certainly a parser for it already. Otherwise, you can use one of the parser generators for PHP (just Google it).

    When you got a parser, everything else is pretty trivial. This also means you won't have to come back for every tiny new feature.

    Comments on this post

    • Laurent_R agrees
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo