Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > get the full string when a word matches

Reply
Thread Tools

get the full string when a word matches

 
 
Rudra Banerjee
Guest
Posts: n/a
 
      09-18-2012
Sorry for posting two problem back to back.
I have a file that contains line like:
(2010) 4287-4293. doi:10.1016/j.physb.2010.07.028
etc.
where the doi:* is of importance to me.
is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      09-19-2012
On 09/18/2012 06:59 PM, Rudra Banerjee wrote:
> Sorry for posting two problem back to back.
> I have a file that contains line like:
> (2010) 4287-4293. doi:10.1016/j.physb.2010.07.028
> etc.
> where the doi:* is of importance to me.
> is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?


Yes, the key tools you'll need are strchr() and strstr(). Precisely how
to use them depends upon precisely what the format is of the string. Do
spaces and newlines delimit the beginning of the doi:, as well as the
end of the string? Can "doi:" ever occur at the beginning of the string?
--
James Kuyper
 
Reply With Quote
 
 
 
 
BruceS
Guest
Posts: n/a
 
      09-19-2012
On Tue, 18 Sep 2012 15:59:57 -0700, Rudra Banerjee wrote:

> Sorry for posting two problem back to back.
> I have a file that contains line like:
> (2010) 4287-4293. doi:10.1016/j.physb.2010.07.028 etc.
> where the doi:* is of importance to me.
> is there any way in C to get the string(even from the middle of line)
> begins with doi: and delimited only by space or newline?


I'm not entirely clear on what you're looking for, but I think strstr()
is probably a good place to start. It will return a pointer to the first
place in a string where another string is found. So, for instance, if
you give it the line above and "doi", you'll get
"doi:10.1016/j.physb.2010.07.028 etc.". If it doesn't find "doi" in the
input string, it will return null, so you'll know the line doesn't
contain "doi". If you want just the part from "doi" up to the first
space, you can then strchr() to find that space and set it to '\0'.
HTH
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-19-2012
On 9/18/2012 6:59 PM, Rudra Banerjee wrote:
> Sorry for posting two problem back to back.
> I have a file that contains line like:
> (2010) 4287-4293. doi:10.1016/j.physb.2010.07.028
> etc.
> where the doi:* is of importance to me.
> is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?


Yes, but your lone example doesn't give enough information
about the pattern ("What's the next number in 9 ...?").

Possible building blocks: The strstr() function to locate
occurrences of "doi:" in the string, the strtok() function to
divide the string into space-separated fields, the sscanf()
function to, well, to scan. And others, of course.

--
Eric Sosman
d
"The speed at which the system fails is usually not important."
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-19-2012
On Tue, 18 Sep 2012 15:59:57 -0700 (PDT), Rudra Banerjee
<> wrote:

>Sorry for posting two problem back to back.
>I have a file that contains line like:
>(2010) 4287-4293. doi:10.1016/j.physb.2010.07.028
>etc.
>where the doi:* is of importance to me.
>is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?


You can use strstr to find the location of the "doi:". If it is
present:

You could use strcspn to find the length of the substring at that
address that does not contain either blank or newline.

You could use strpbrk to find the location of the first blank or
newline after the "doi:".

You could use strtok to terminate the substring at the first
blank or newline after the "doi:"

--
Remove del for email
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      09-19-2012
בתאריך יום שלישי, 18 בספטמבר 2012 23:59:57 UTC+1, מאת Rudra Banerjee:
> Sorry for posting two problem back to back.
>
> is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?


this gives you the rough idea.

char *doi;
char *end;
size_t len;
char *answer;

doi = strstr(str, "doi:");
end = doi;
while(*end && !isspace( (unsigned) *end))
end++;
len = end - doi - 4;
answer = malloc(len + 1);
memcpy(answer, doi+4, len);
answer[len] = 0;


you need to decide what to do if the string doesn't contain doi, contains two
or if the date is clearly not a vaild doi.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-19-2012
Malcolm McLean <> writes:

> בתאריך יום שלישי, 18 בספטמבר 2012 23:59:57 UTC+1, מאת Rudra Banerjee:
>> Sorry for posting two problem back to back.
>>
>> is there any way in C to get the string(even from the middle of line) begins with doi: and delimited only by space or newline?

>
> this gives you the rough idea.
>
> char *doi;
> char *end;
> size_t len;
> char *answer;
>
> doi = strstr(str, "doi:");
> end = doi;
> while(*end && !isspace( (unsigned) *end))


I think you meant (unsigned char) here.

> end++;


<snip>
--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if String.matches() AND (if yes) extract number from String? Jochen Brenzlinger Java 5 11-21-2011 07:43 PM
I have to delete the whole line in the file (first word matches), how can I ? santa19992000@yahoo.com C Programming 6 12-03-2005 12:53 PM
String matches() method Sharp Java 5 05-02-2005 11:52 AM
use matches in string w/o Pattern rocalp Java 0 02-18-2004 05:24 AM
searching for matches within a word list Rajarshi Guha Python 1 07-30-2003 10:10 PM



Advertisments