Problem on understanding the regexp command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem on understanding the regexp command
# 1  
Old 12-16-2012
Problem on understanding the regexp command

Hi all,

I'm not clear of this regexp command:

Code:
regexp {(\S+)\/[^\/]+$} $String match GetString

From my observation and testing,
if $String is abc/def/gh

$GetString will be abc/def

I don't understand how the /gh in $String got eliminated.
Please help. Thanks
# 2  
Old 12-16-2012
I ran this regex in PERL to show you what exactly is happening:-
Code:
#!/usr/bin/perl

my $str = 'abc/def/gh';
my @r_1 = $str =~ /(\S+)\//;
my @r_2 = $str =~ /\/(\S+)/;
print "String: ", $str, "\nRegex 1: ", @r_1, "\nRegex 2: ", @r_2, "\n";

Code:
./regex.pl
String: abc/def/gh
Regex 1: abc/def
Regex 2: def/gh

In 1st regex, the string is getting split considering last / sign.
In 2nd regex, the string is getting split considering first / sign.
So basically whatever is there before the first & last / is getting omitted. I hope you understood.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-16-2012
Thanks bipinajith for helping..
There are slight differences between perl and tcl's regex function in this case. Nevertheless, thanks to your sharing, I think I've figured out the meaning of this tcl's regex function code:

Code:
regexp {(\S+)\/[^\/]+$} $String match GetString

Here's what I figured out:

The first bracket (\S+), means to search for characters or numbers or symbols except whitespaces (spacebar or tab). This means that at this phase, the searched pattern is abc/def/gh.

The next phase \/[^\/]+$ is to search for the last / plus one or more characters or numbers or symbols except the / symbol till the end of the string. Only the matched pattern inside the ( ) will be copied to GetString which means in this case abc/def

This is what I found out, please comment if I'm wrong.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with (Understanding) function

I have this code #!/bin/bash LZ () { RETVAL="\n$(date +%Y-%m-%d_%H-%M-%S) --- " return RETVAL } echo -e $LZ"Test" sleep 3 echo -e $LZ"Test" which I want to use to make logentrys on my NAS. I expect of this code that there would be output like 2017-03-07_11-00-00 --- Test (4 Replies)
Discussion started by: matrois
4 Replies

2. Shell Programming and Scripting

Problem in understanding debugging

Hi i was going through the script debugging technique. below example was given in the book. 1 #!/bin/sh 2 3 Failed() { 4 if ; then 5 echo "Failed. Exiting." ; exit 1 ; 6 fi 7 echo "Done." 8 } 9 10 echo "Deleting old backups,... (11 Replies)
Discussion started by: scriptor
11 Replies

3. Shell Programming and Scripting

Regarding regexp command

Hello, I'm reviewing a tcl script, and for this line: regexp {(\S+)\/+$} $string match $catch if,$string == ab123c/de456f/try99/zxy is there any possibility that $catch == ab123c/de456f/try99 ? or it must only be $catch == ab123c/de456f/try99/zxy ? Please advise. Thanks (1 Reply)
Discussion started by: mar85
1 Replies

4. Shell Programming and Scripting

Problem in understanding export uses

i am beginner in shell scripting. not able to understand what below line will do. PS1=${HOST:=Žuname -nŽ}"$ " ; export PS1 HOST below is the script #!/bin/hash PS1=${HOST:=Žuname -nŽ}"$ " ; export PS1 HOST ; echo $PS1 and i getting the below output Žuname -nŽ$ (25 Replies)
Discussion started by: scriptor
25 Replies

5. UNIX for Dummies Questions & Answers

Problem understanding Paths

If I don't explain my issue well enough, I apologize ahead of time, extreme newbie here to scripting. I'm currently learning scripting from books and have moved on to the text Wicked Cool Shell Scripts by Dave Taylor, but there are still basic concepts that I'm having trouble understanding. ... (10 Replies)
Discussion started by: Chasman78
10 Replies

6. Shell Programming and Scripting

Problem with the shell script for understanding

Can Anybody please tell me the meaning of the script: #!/bin/sh str=$@ echo $str | sed 's/.*\\//' exit 0 (6 Replies)
Discussion started by: nixhead
6 Replies

7. Shell Programming and Scripting

Regexp and sed problem

Basically it should identify what ever is in between /*< >*/ (tags) and replace dbname ending with (.) with the words in between the tags i.e. DELETE FROM /*<workDB>*/epd_test./*<multi>*//*<version>*/epd_tbl1 ALL; into DELETE FROM... (4 Replies)
Discussion started by: sol_nov
4 Replies

8. UNIX for Dummies Questions & Answers

print the line immediately after a regexp; but regexp is a sentence

Good Day, Im new to scripting especially awk and sed. I just would like to ask help from you guys about a sed command that prints the line immediately after a regexp, but not the line containing the regexp. sed -n '/regexp/{n;p;}' filename What if my regexp is 3 word or a sentence. Im... (3 Replies)
Discussion started by: ownins
3 Replies

9. Shell Programming and Scripting

Problem with regexp for IP-Adress Pattern

Hi all Unix Gurus! Since hours (even days :-)) I'm trying to find the correct pattern to search for IP addesses in text files. The pattern to find a IP address itself is not too difficult: '((||1{2}|2|2{2})\.){3,}(||1{2}|2|2{2})' BUT, of course the above pattern is also matching lines like... (9 Replies)
Discussion started by: desertchannel
9 Replies

10. Shell Programming and Scripting

egrep understanding problem

Hi, Can anyone please let me know the meaning of this line,i am not able to understand the egrep part(egrep '^{1,2}).This will search for this combination in beginning but what does the values in {}signifies here. /bin/echo $WhenToRun | egrep '^{1,2}:$' >/dev/null (1 Reply)
Discussion started by: namishtiwari
1 Replies
Login or Register to Ask a Question