Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 20th, 2013, 07:10 PM
Simon Lloyd Simon Lloyd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Simon Lloyd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 49 sec
Reputation 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

Reply With Quote
  #2  
Old February 21st, 2013, 12:40 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 506 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 5 m 2 sec
Reputation Power: 385
I don't understand what you want to do. Your desired result looks the same as the input. What are you trying to do?

Reply With Quote
  #3  
Old February 21st, 2013, 01:07 PM
Simon Lloyd Simon Lloyd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Simon Lloyd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 49 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #4  
Old February 21st, 2013, 09:01 PM
spacebar208's Avatar
spacebar208 spacebar208 is online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 190 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 41 m 46 sec
Reputation 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

Reply With Quote
  #5  
Old February 21st, 2013, 10:12 PM
Simon Lloyd Simon Lloyd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Simon Lloyd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 49 sec
Reputation 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

Reply With Quote
  #6  
Old February 22nd, 2013, 01:01 AM
spacebar208's Avatar
spacebar208 spacebar208 is online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 190 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 41 m 46 sec
Reputation 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); 

Reply With Quote
  #7  
Old February 22nd, 2013, 07:50 AM
Simon Lloyd Simon Lloyd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Simon Lloyd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 49 sec
Reputation 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

Reply With Quote
  #8  
Old February 28th, 2013, 02:44 AM
Simon Lloyd Simon Lloyd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Simon Lloyd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 49 sec
Reputation 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?

Reply With Quote
  #9  
Old March 3rd, 2013, 04:26 PM
spacebar208's Avatar
spacebar208 spacebar208 is online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 190 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 41 m 46 sec
Reputation 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

Reply With Quote
  #10  
Old March 4th, 2013, 11:17 AM
Simon Lloyd Simon Lloyd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Simon Lloyd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 49 sec
Reputation 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?

Reply With Quote
  #11  
Old March 4th, 2013, 12:25 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,863 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 19 h 44 m 40 sec
Reputation Power: 813
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Replacing words in string

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap