The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Replacing words in string
Discuss Replacing words in string in the Regex Programming forum on Dev Shed. Replacing words in string Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 20th, 2013, 07:10 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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
|

February 21st, 2013, 12:40 PM
|
|
|
|
I don't understand what you want to do. Your desired result looks the same as the input. What are you trying to do?
|

February 21st, 2013, 01:07 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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 
|

February 21st, 2013, 09:01 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
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', '\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]
|

February 21st, 2013, 10:12 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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\\2 \\3 End \\2[/KODE]' . "\n", $data);
or has your solution not been copied over correctly?
Like i said this is unkown territory to me 
|

February 22nd, 2013, 01:01 AM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
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);
|

February 22nd, 2013, 07:50 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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 
|

February 28th, 2013, 02:44 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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?
|

March 3rd, 2013, 04:26 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
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]
|

March 4th, 2013, 11:17 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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?
|

March 4th, 2013, 12:25 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|