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:
Regular expression for tab separator

 



masaniparesh
Novice

Jul 26, 2009, 10:10 PM

Post #1 of 9 (5665 views)
Regular expression for tab separator Can't Post

Hi,

Could anyone please help me to write regular expression for getting two string(with spaces) which are tab separator.

For example:

/foo/bar baz/my test1 '/t' /foo/bar baz/my test2

The regular expression should return "/foo/bar baz/my test1" in $1 and "/foo/bar baz/my test2" in $2.

Thanks.


ichi
User

Jul 27, 2009, 1:39 AM

Post #2 of 9 (5664 views)
Re: [masaniparesh] Regular expression for tab separator [In reply to] Can't Post

you just need to split on tab


Code
$str = "/foo/bar baz/my test1\t/foo/bar baz/my test2"; 
@s = split /\t/ , $str;

not tested, but you get the idea.


masaniparesh
Novice

Jul 27, 2009, 2:12 AM

Post #3 of 9 (5663 views)
Re: [ichi] Regular expression for tab separator [In reply to] Can't Post

Thanks Ichi, you are correct but before that I need to verify that the string is separated by /t or not. so for that I have to use regular expression. I am using below code but it returns only /proj in $1.

my $line = "/proj/bar baz/with space dir /proj/bar baz";
my $my_re = '(?:(\/\w+))';
my $match = $my_re . '/t' . '{0,1}' . $my_re . '{0,1}';
if( $line =~ /$match/g ) {
print "matched\n";
print "$1 <==> $2\n";
} else {
print "not matched\n";
}


Basically I want "/proj/bar baz/with space dir" in $1 and "/proj/bar baz" in $2.

Thanks.


ichi
User

Jul 27, 2009, 2:17 AM

Post #4 of 9 (5661 views)
Re: [masaniparesh] Regular expression for tab separator [In reply to] Can't Post

you can just do the split as i suggested, then if the number of elements returned in @s is more than 1, that will suggest to you that there is a tab in your string. isn't that so?


masaniparesh
Novice

Jul 27, 2009, 2:22 AM

Post #5 of 9 (5660 views)
Re: [ichi] Regular expression for tab separator [In reply to] Can't Post

No, actually there are multiple string formats available in file and one of them what I have given so I have to match the string before doing anything. The regular expression usage would be mandatory in my case.

Thanks.


masaniparesh
Novice

Jul 27, 2009, 2:26 AM

Post #6 of 9 (5659 views)
Re: [ichi] Regular expression for tab separator [In reply to] Can't Post

If there is no space in both of string and separator is space then below working fine but problem arise when space is there in either or both strings. The second string might not be available but if it is available then should be separated by space.

my $my_re1 = '(?: (\/\w\S+))';
my $my_re2 = '(?:\s+(\/\w\S+))';

$match = $my_re1 . $my_re2 . '{0,1}'

Now I want to support above functionality with having space in either or both string and separated by tab.

Hope you understood now.

Thanks for being there.


KevinR
Veteran


Jul 27, 2009, 9:25 AM

Post #7 of 9 (5651 views)
Re: [masaniparesh] Regular expression for tab separator [In reply to] Can't Post

please start using the code tags to post code and samples of data, that way spaces and tabs are preserved in the display making it much easier to see what you are trying to explain.
-------------------------------------------------


perllearner
Novice

Jul 30, 2009, 5:32 AM

Post #8 of 9 (5624 views)
Re: [KevinR] Regular expression for tab separator [In reply to] Can't Post

Hi,


I am not able to understand this part of code in your thread. Can you please explain this to me?

my $my_re1 = '(?: (\/\w\S+))

Thanks in advance...


spider
User


Aug 6, 2009, 5:13 AM

Post #9 of 9 (5499 views)
Re: [masaniparesh] Regular expression for tab separator [In reply to] Can't Post

Have not studied everything in detailed, but noticed that you are refering to tab as /t.
Try with \t insted.

 
 


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

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