CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [hwnd] Find and replace: Edit Log



Zhris
Enthusiast

Aug 24, 2013, 9:32 PM


Views: 3261
Re: [hwnd] Find and replace

Hi,

You could possibly perform this task in multiple steps ( three or four separate regexps ).

Of course, with real world data, you may bump into issues that haven't been represented by your example data, therefore I haven't supported i.e. only supports one set of parenthesis per < > wrapper, no support for unmatched parenthesis etc etc etc.

Breakdown (Perl-ified):

Code
>>> Substitute desirable parenthesis with another bracket type char set, perform core quotification, then re-substitute <<< 

1. Find unique brackets that are not wrapped in triangle brackets. I have tested for square brackets.
m/<(.*?)[\[\]](.*?)>/

2. Replace parenthesis brackets wrapped in triangle brackets with square brackets.
s/(<(?:.*?))\((.*?)\)((?:.*?)>)/$1\[$2\]$3/g

3. Quotify leftover parenthesis brackets.
s/\((.*?)\)/'$1'/g

4. Replace square brackets wrapped in triangle brackets with parenthesis brackets.
s/(<(?:.*?))\[(.*?)\]((?:.*?)>)/$1($2)$3/g


Code (pure Perl):

Code
my $str = "this (is) a test of <a te(st)ing> of a (type)"; 

die "not unique enough\n" if ( $str =~ m/<(.*?)[\[\]](.*?)>/ );

$str =~ s/(<(?:.*?))\((.*?)\)((?:.*?)>)/$1\[$2\]$3/g;
$str =~ s/\((.*?)\)/'$1'/g;
$str =~ s/(<(?:.*?))\[(.*?)\]((?:.*?)>)/$1($2)$3/g;

print $str;


My preference would be to do this in Perl ;). The example below is rough / probably inefficient, but is provided to represent the comparatively ease of this approach. It is also likely to support real world data more desirably:

Code
my $str = "this (is) a test of <a te(st)ing> of a (type)"; 
$str =~ s/(.)/quotify($1)/eg;
print $str;

sub quotify
{
my $c = shift;
$c = "'" unless (($c eq '<' .. $c eq '>') || $c !~ /[()]/);
return $c;
}


Finally, could it possible to use lookahead / lookbehind assertions to produce a single regexp (I need to brush up on these).

Hope this helps.

Chris


(This post was edited by Zhris on Aug 24, 2013, 10:40 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 9:38 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 9:39 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 9:41 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 9:41 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 9:41 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 9:43 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 9:48 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 9:48 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 9:53 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 9:57 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:02 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:03 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:09 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:11 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:12 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:12 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:13 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:15 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:16 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:16 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:17 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:20 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:24 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:27 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:27 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:31 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:33 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:34 PM
Post edited by Zhris (Enthusiast) on Aug 24, 2013, 10:40 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives