Results 1 to 6 of 6
Hi,
I have a string "aaa://abcxyz/xxxxx"
I have to get strings "aaa", "abcxyz" and "/xxxxx" from the string I have.
Can you please help me out with this?
Thanks and ...
- 06-05-2009 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 13
collecting substrings from the string
Hi,
I have a string "aaa://abcxyz/xxxxx"
I have to get strings "aaa", "abcxyz" and "/xxxxx" from the string I have.
Can you please help me out with this?
Thanks and Regards,
krishna
- 06-05-2009 #2Linux Newbie
- Join Date
- Nov 2007
- Location
- Planet Earth
- Posts
- 152
Hi,
Which language? ... Anyway, the best solution will be based on regular expressions, using parenthesis for memorize the part of the string. For example, in JS you can do:
and you will have an array with the parts of the expression... there are similar functionalities in almos all modern languages.Code:var parts = /(\w+):\/\/(\w+)(\/\w+)/.exec(pattern)
HugoEOF
- 06-05-2009 #3Just Joined!
- Join Date
- Oct 2008
- Posts
- 13
Hi,
The language is C. I tried the following and it is working.
string httpUrl("aaa://abcxyz/xxxxx");
string hostname;
char *local_path = NULL;
char *protocol = NULL;
char *ip = NULL;
protocol = strstr(httpUrl.c_str(), "//");
protocol++;
protocol++;
local_path = strstr(protocol, "/");
hostname = httpUrl.substr(protocol - httpUrl.c_str(),local_path - protocol);
printf("\nprotocol is %s and length is %d\n",protocol,strlen(protocol));
printf("\nlocal_path is %s and length is %d\n",local_path,strlen(local_path));
printf("\nhostname is %s and length is %d\n",hostname.c_str(),strlen(hostname.c_str()));
I just want to know if there is any better way to do this.
Thanks and Regards,
krishnaLast edited by krishna chaithanya; 06-05-2009 at 07:03 AM. Reason: changing the string name
- 06-05-2009 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 06-05-2009 #5Just Joined!
- Join Date
- Apr 2009
- Posts
- 33
you could use sed, eg
str="aaa://abcxyz/xxxxx"
str1=$(echo $str| sed's?^\([^:]*\):.*$?\1?')
str2=$(echo $str| sed 's?^[^/]*//\([^/]*\)/.*$?\1?')
str3=$(echo $str| sed 's?^[^/]*//[^/]*\(/.*\)$?\1?')
your first substring is stored in $str1, second substring in $str2, etc
- 06-05-2009 #6Linux User
- Join Date
- Aug 2006
- Posts
- 458
no need to call sed so many times,
in fact, if using bash, no need to call external commandsCode:# echo "aaa://abcxyz/xxxxx" | sed 's!\(.*\)://\(.*\)/\(.*\)!\1 \2 \3!' aaa abcxyz xxxxx
Code:# string="aaa://abcxyz/xxxxx" # IFS="/" # set -- $string # echo $1 aaa: # echo $3 abcxyz # echo $4 xxxxx


Reply With Quote
