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: Regular Expressions:
Need help in tab separator string

 



masaniparesh
Novice

Jul 28, 2009, 5:39 AM

Post #1 of 14 (7801 views)
Need help in tab separator string Can't Post

Hi, Could anyone please help me here. I have been facing problems in getting two string separated by one and only one tab. The below code also works fine if there are more than one tab separator. I want to take all words before tab character in first string and all words after tab folder in second string. At the same time there must be only one tab separator. Moreover, if there is no tab in string then eveything should go in first string.

Code
   my $line = "/proj/bar baz/with space dir /proj/bar baz"; 
my $match = '^(.*)' . '\t(.*){0,1}';
if( $line =~ /$match/ )
{
print "matched\n";
print "not defined\n" if(!defined($2) || $2 eq "");
print "$1 <==> $2\n";
} else
{
print "not matched\n";
}


Thanks.


shawnhcorey
Enthusiast


Jul 28, 2009, 8:48 AM

Post #2 of 14 (7795 views)
Re: [masaniparesh] Need help in tab separator string [In reply to] Can't Post

I think this is what you want.

Code
my ( $first, $rest ) = split /\t/, $line, 2;


See `perldoc -f split` for details.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


masaniparesh
Novice

Jul 28, 2009, 8:52 AM

Post #3 of 14 (7793 views)
Re: [shawnhcorey] Need help in tab separator string [In reply to] Can't Post

Actually I have been asking for regex. I really don't want to use split for reasons.

Thanks.


FishMonger
Veteran / Moderator

Jul 28, 2009, 9:06 AM

Post #4 of 14 (7790 views)
Re: [masaniparesh] Need help in tab separator string [In reply to] Can't Post

The only valid reason for not using split would be that it's a requirement for a homework assignment.

If that's the case, we can't provide the answer to a homework assignment.


shawnhcorey
Enthusiast


Jul 28, 2009, 9:28 AM

Post #5 of 14 (7788 views)
Re: [masaniparesh] Need help in tab separator string [In reply to] Can't Post


In Reply To
Actually I have been asking for regex. I really don't want to use split for reasons.

Thanks.


The split does contain a regex.

And if this is a homework assignment, your instructor's a jerk. Best practises says that split is the best for this job and your instructor should be teaching that.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


masaniparesh
Novice

Jul 28, 2009, 9:40 AM

Post #6 of 14 (7786 views)
Re: [shawnhcorey] Need help in tab separator string [In reply to] Can't Post

Hello, I have genuine reason for that and I know Perl pretty well but its complicated case for me. I just didn't want to give my complication scenario here.

I have been reading some strings from the file that contains many different formats and out of them one of the format I have given in my main question. The issue is I can use split only after I recognize the pattern. So to recognize each unique pattern, I have to use REGEX.

For example; it is like below

While(<file>) {

if(/regex1/) {
...do task...
} elsif(/regex2/) {
...do task...
} elsif(/regex3/) {
...do task...
} and so on.......

Now if you understood the issue then could you tell me whether it is possible to write regex for my requirement or not.

Thanks.


masaniparesh
Novice

Jul 28, 2009, 9:43 AM

Post #7 of 14 (7785 views)
Re: [FishMonger] Need help in tab separator string [In reply to] Can't Post

Moreover, it is not home assignment. :-) I want to be compatible with my previous version of comparing the string using regex. Do you see any other way of doing it?

I could check for existence of /t but that would be poor solution as many line can contain /t but not the format I mentioned in my main question.

I hope you people understood the issue.

Thanks.


KevinR
Veteran


Jul 28, 2009, 10:41 AM

Post #8 of 14 (7779 views)
Re: [masaniparesh] Need help in tab separator string [In reply to] Can't Post

If you want to test if a string has only one tab, don't use a regexp, use the tr operator (It looks like a regexp but its not):


Code
@array = ("test with one tab\t test", "test with two tabs\t\ttest");  
for (@array) {
if (1 == tr/\t/\t/) {
print "[$_ ] There is only one tab\n";
}
else {
print "[$_] There is more than one tab\n";
}
}

-------------------------------------------------


(This post was edited by KevinR on Jul 28, 2009, 10:41 AM)


KevinR
Veteran


Jul 28, 2009, 10:46 AM

Post #9 of 14 (7776 views)
Re: [masaniparesh] Need help in tab separator string [In reply to] Can't Post

Or using a regexp:


Code
@array = ("test with one tab\ttest", "test with two tabs\t\ttest");  
for (@array) {
if (/^([^\t]+)\t([^\t]+)$/) {
print "$1 $2\n";
}
else {
print "Did not match\n";
}
}

-------------------------------------------------


masaniparesh
Novice

Jul 28, 2009, 9:49 PM

Post #10 of 14 (7765 views)
Re: [KevinR] Need help in tab separator string [In reply to] Can't Post

Perfect Kevin....!! Thats what I was looking for. Thanks a lot.


(This post was edited by masaniparesh on Jul 29, 2009, 2:54 AM)


masaniparesh
Novice

Jul 28, 2009, 9:58 PM

Post #11 of 14 (7763 views)
Re: [KevinR] Need help in tab separator string [In reply to] Can't Post

Kevin, small question. Your code works fine but is it possible to put restriction on trailing spaces. Note the first string has spaces before /t but still it will get succeed. In this case it should say "did not match. I could remove trailing spaces inside the matching statement but just wondering if it is possible to restrict it using REGEX.

Code
@array = ("test with one tab   \ttest", "test with two tabs\t\ttest");   
for (@array) {
if (/^([^\t]+)\t([^\t]+)$/) {
print "$1 $2\n";
}
else {
print "Did not match\n";
}
}

Thanks.



KevinR
Veteran


Jul 29, 2009, 10:19 AM

Post #12 of 14 (7756 views)
Re: [masaniparesh] Need help in tab separator string [In reply to] Can't Post

Maybe this will do what you need:"


Code
@array = ("test with one tab no space\ttest","test with one tab and space   \ttest", "test with two tabs\t\ttest");    
for (@array) {
if (/^([^\t]+)(?<! )\t([^\t]+)$/) {
print "$1 $2\n";
}
else {
print "Did not match\n";
}
}


?<! is a zero-width negative look behind assertion. Zero-width assertions don't capture and store patterns in memory so $2 is not affected, in other words you don't have to use $3 to capture and store what is inside the third set of parentheses.

Some reading:

http://perldoc.perl.org/perlretut.html#Looking-ahead-and-looking-behind
-------------------------------------------------


(This post was edited by KevinR on Jul 29, 2009, 10:20 AM)


masaniparesh
Novice

Jul 29, 2009, 11:59 AM

Post #13 of 14 (7747 views)
Re: [KevinR] Need help in tab separator string [In reply to] Can't Post

Thanks Kevin. I have missed one statement from my main question. "Moreover, if there is no tab in string then eveything should go in first string." That means the regular expression should also validate the string which doesn't have tab at all. I did it below way. It works fine but it doesn't meet one requirement "if there is a tab then there must be a non-empty string after that". I want "test with tab only /t" string to get unmatched but "test without tab" should be matched.Thanks.

Code
@array = ("test with no tab test");     
for (@array) {
if (/^([^\t]+)(?<! )\t{0,1}([^\t]+){0,1}$/) {
print "$1 $2\n";
}
else {
print "Did not match\n";
}
}




KevinR
Veteran


Jul 29, 2009, 12:23 PM

Post #14 of 14 (7743 views)
Re: [masaniparesh] Need help in tab separator string [In reply to] Can't Post

What you want to do is not best served by trying to write one rather complex regexp, you should use two or maybe even three and use them in a hierarchical order of importance. Something like this is more practical:


Code
my @array = ("test with one tab no space\ttest", 
"test with one tab and space \ttest",
"test with two tabs\t\ttest",
"test with no tab",
"",
"test with nothing after tab\t",
"test with one tab no space\tand non empty string after tab"
);



for (@array) {
if (length($_) > 0 && !/\t/) {
print "Matched first condition: [$_]\n";
}
elsif (/^([^\t]+)(?<! )\t([^\t]+)$/) {
print "Matched second condition: [[$1] [$2]]\n";
}
else {
print "Did not match at all: [$_]\n";
}
}

-------------------------------------------------

 
 


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

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