[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

how to use regex library

Owner

5/18/2011 3:12:00 AM

I wanted regex lib, so I googled and found henry spencer's

regex lib. I did tried look into try.c. but I need more

examples of how to use the regex lib. Can someone help me

with this please?

Thank you


64 Answers

Seebs

5/18/2011 3:33:00 AM

0

On 2011-05-18, Owner <Owner@Owner-PC.com> wrote:
> I wanted regex lib, so I googled and found henry spencer's
> regex lib. I did tried look into try.c. but I need more
> examples of how to use the regex lib. Can someone help me
> with this please?

This is too vague. What do you want to do? What on earth are you
using that doesn't already have a working regex library, that you
need to go look one up?

The usual way people go about using a library is that they start by
knowing what they want to do, then they look for things that can do it.
You seem to be starting by grabbing random software, then trying to think
of things you can do with it. I suggest that perhaps you would be less
confused if you did things in the other way.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Owner

5/18/2011 10:41:00 AM

0

On Wed, 18 May 2011 03:33:28 +0000, Seebs wrote:

> On 2011-05-18, Owner <Owner@Owner-PC.com> wrote:
>> I wanted regex lib, so I googled and found henry spencer's
>> regex lib. I did tried look into try.c. but I need more
>> examples of how to use the regex lib. Can someone help me
>> with this please?
>
> This is too vague. What do you want to do? What on earth are you
> using that doesn't already have a working regex library, that you
> need to go look one up?
>
> The usual way people go about using a library is that they start by
> knowing what they want to do, then they look for things that can do it.
> You seem to be starting by grabbing random software, then trying to think
> of things you can do with it. I suggest that perhaps you would be less
> confused if you did things in the other way.
>
> -s

I apologize for vagueness. I'm asking to anyone knows how to use

henry spencer's regex library. I expected to send RE pattern

and target string to the library, then it send me back with either

match was found of not, if found send me back position in the

target string and length of matched string. but when I saw

the only file with main(); try.c, quickly I got confused.

I got lost and don't even know how to use the henry spencer's

lib functions(even though I have copy of it). that's why I called

for help. Just getting a small clue wouldbe make this question

wealthy. thank you

James Kuyper

5/18/2011 12:00:00 PM

0

On 05/18/2011 06:41 AM, Owner wrote:
> On Wed, 18 May 2011 03:33:28 +0000, Seebs wrote:
>
>> On 2011-05-18, Owner <Owner@Owner-PC.com> wrote:
>>> I wanted regex lib, so I googled and found henry spencer's
>>> regex lib. I did tried look into try.c. but I need more
>>> examples of how to use the regex lib. Can someone help me
>>> with this please?
>>
>> This is too vague. What do you want to do? What on earth are you
>> using that doesn't already have a working regex library, that you
>> need to go look one up?
>>
>> The usual way people go about using a library is that they start by
>> knowing what they want to do, then they look for things that can do it.
>> You seem to be starting by grabbing random software, then trying to think
>> of things you can do with it. I suggest that perhaps you would be less
>> confused if you did things in the other way.
>>
>> -s
>
> I apologize for vagueness. I'm asking to anyone knows how to use
> henry spencer's regex library. I expected to send RE pattern
> and target string to the library, then it send me back with either
> match was found of not, if found send me back position in the
> target string and length of matched string. but when I saw
> the only file with main(); try.c, quickly I got confused.
> I got lost and don't even know how to use the henry spencer's
> lib functions(even though I have copy of it). that's why I called
> for help. Just getting a small clue wouldbe make this question
> wealthy. thank you

You've completely ignored the question that was asked, which was "what
do you want to do?", and simply provided a more detailed answer to the
question "how do you want to do it?".

Example of "what do you want to do?": "I want to add the values stored
in 'a' and 'b', and put the result in 'c'.
Example of a defective answer to "how do you want to do it?": "I want to
write c *= a+b;"
Do you see how hard it is to explain what's wrong with the second
answer, without first knowing the answer to the first question?

Please explain what you want to do. If your answer contains, anywhere
within it, the words "regex", "library", "henry", "spencer", "RE", or
"try.c", then you're still answering the wrong question - please rewrite it.
--
James Kuyper

Mark Bluemel

5/18/2011 12:36:00 PM

0

On 05/18/2011 11:41 AM, Owner wrote:

> I apologize for vagueness. I'm asking to anyone knows how to use
> henry spencer's regex library.

I didn't, but having looked at it briefly, I think I could do so now.

Have you tried reading the manual page? The principles seem fairly
straightforward - the crucial thing you need to understand is that your
expectation is wrong.

> I expected to send RE pattern
> and target string to the library, then it send me back with either
> match was found of not, if found send me back position in the
> target string and length of matched string.

Why did you expect this? You should try and understand the API as
implemented and documented rather than what you hope it might be.

Key points (illustrated from the example program):-

1) the regular expression needs to be compiled before use

#include <regexp.h>
regexp *r;
r = regcomp(argv[1]); /* compile RE from command line*/

2) there may be multiple parenthesized expressions to be matched, so you
can't expect to only have a single position and length.
(There can be up to NSUBEXP that the library will handle, NSUBEXP
defaults to 10, but can be changed by recompilation)

int i;
/* did the string from the command line match ? */
i = regexec(r, argv[2]);
printf("%d", i);
for (i = 1; i < NSUBEXP; i++)
/* check start and end pointers for matched expressions */
if (r->startp[i] != NULL && r->endp[i] != NULL)
printf(" \\%d", i);

Jorgen Grahn

5/18/2011 2:05:00 PM

0

On Wed, 2011-05-18, Seebs wrote:
> On 2011-05-18, Owner <Owner@Owner-PC.com> wrote:
>> I wanted regex lib, so I googled and found henry spencer's
>> regex lib. I did tried look into try.c. but I need more
>> examples of how to use the regex lib. Can someone help me
>> with this please?
>
> This is too vague. What do you want to do? What on earth are you
> using that doesn't already have a working regex library, that you
> need to go look one up?

You may need to look it up to know that you have it already. Henry
Spencer's routines are part of Gnu libc, but googling is surely an
honorable way of discovering that.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Seebs

5/18/2011 4:04:00 PM

0

On 2011-05-18, Owner <Owner@Owner-PC.com> wrote:
> I apologize for vagueness. I'm asking to anyone knows how to use
> henry spencer's regex library.

Well, sure, but again... What on earth are you using that doesn't have
one already?

> I expected to send RE pattern
> and target string to the library, then it send me back with either
> match was found of not, if found send me back position in the
> target string and length of matched string.

That is indeed what it does.

> but when I saw
> the only file with main(); try.c, quickly I got confused.

This is not specific.

> I got lost and don't even know how to use the henry spencer's
> lib functions(even though I have copy of it). that's why I called
> for help. Just getting a small clue wouldbe make this question
> wealthy. thank you

Okay, here's the clue:

http://www.catb.org/~esr/faqs/smart-ques...

Read that. Think about it. Then try to ask a smart question.

Here's some food for thought:

* Why don't you start by reading the documentation?
* If you have access to any sort of Unix-like system, consider reading the
docs for the regular expression library the system *already has*.
* In fact, if Windows systems don't have one too, that would sort of surprise
me, this is very basic functionality.
* What you may be missing is that taking a regular expression string and
converting it into something that can be quickly and efficiently processed
is computationally expensive...
* ... and most programs which use a regular expression use it more than once.
* Because of this, regex libraries typically separate out the "compilation"
of a regex from a string into an internal data structure, and the use of
that data structure to match stuff.

But seriously, this question is really too vague to do anything with. You
don't give us any clue what you want to do, what you've tried, or whether
you even know what "documentation" is, let alone whether you've read it.
Saying that you are confused by reading example code suggests that you haven't
yet looked at documentation.

Try using regcomp, regexec, and then regfree, in that order. If that's
not enough of a clue, you really are going to have to learn to ask better
questions.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Owner

5/18/2011 5:13:00 PM

0

This might help a lot.

Thank you!

Ben Bacarisse

5/18/2011 7:00:00 PM

0

James Kuyper <jameskuyper@verizon.net> writes:
<snip>
> Please explain what you want to do. If your answer contains, anywhere
> within it, the words "regex", "library", "henry", "spencer", "RE", or
> "try.c", then you're still answering the wrong question - please rewrite it.

I agree that the OP has not answered the question the Seebs asked, but I
don't see why he or she must answer it. What's wrong with asking (in a
round about way) "can you help me use the XYZ library?".

--
Ben.

Seebs

5/18/2011 7:45:00 PM

0

On 2011-05-18, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> James Kuyper <jameskuyper@verizon.net> writes:
><snip>
>> Please explain what you want to do. If your answer contains, anywhere
>> within it, the words "regex", "library", "henry", "spencer", "RE", or
>> "try.c", then you're still answering the wrong question - please rewrite it.

> I agree that the OP has not answered the question the Seebs asked, but I
> don't see why he or she must answer it. What's wrong with asking (in a
> round about way) "can you help me use the XYZ library?".

The lack of any indication of what the problem is. Consider the following
questions I might have about a regex library:

* How do I link this with my program?
* Why does a binary I built with this not run on other machines?
* How do I build with this if I don't have root privileges for the install?
* Any time I try to refer to the data types for this library, I get
complaints about unknown types, what am I doing wrong?
* I can't understand the difference between basic and extended regular
expressions, can someone tell me?
* Do I have to call regfree() every time I use a regular expression, or just
once when I'm done with it?
* If I never call regfree(), do I have a memory leak?
* What does regcomp() actually do?
* What arguments should I provide to regcomp()?
* What's the difference between regcomp() and regexec()?

All of these might be ways in which I needed help using it. I could write
a (short) book of information on using Spencer's regex library, and still not
have hit the specific problem the OP might or might not have been having.

What's lacking is any indication of what the OP wants to do, and what part
of it isn't working or making sense. We don't know whether the OP has read
the documentation (although I'm guessing not), or ever reads documentation,
or actually knows what a regex is, or is on a platform where these functions
are not a part of the system library, or anything.

No place to start.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Ben Bacarisse

5/18/2011 11:44:00 PM

0

Seebs <usenet-nospam@seebs.net> writes:

> On 2011-05-18, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>> James Kuyper <jameskuyper@verizon.net> writes:
>><snip>
>>> Please explain what you want to do. If your answer contains, anywhere
>>> within it, the words "regex", "library", "henry", "spencer", "RE", or
>>> "try.c", then you're still answering the wrong question - please rewrite it.
>
>> I agree that the OP has not answered the question the Seebs asked, but I
>> don't see why he or she must answer it. What's wrong with asking (in a
>> round about way) "can you help me use the XYZ library?".
>
> The lack of any indication of what the problem is. Consider the following
> questions I might have about a regex library:
>
> * How do I link this with my program?
> * Why does a binary I built with this not run on other machines?
> * How do I build with this if I don't have root privileges for the install?
> * Any time I try to refer to the data types for this library, I get
> complaints about unknown types, what am I doing wrong?
> * I can't understand the difference between basic and extended regular
> expressions, can someone tell me?
> * Do I have to call regfree() every time I use a regular expression, or just
> once when I'm done with it?
> * If I never call regfree(), do I have a memory leak?
> * What does regcomp() actually do?
> * What arguments should I provide to regcomp()?
> * What's the difference between regcomp() and regexec()?
>
> All of these might be ways in which I needed help using it. I could write
> a (short) book of information on using Spencer's regex library, and still not
> have hit the specific problem the OP might or might not have been
> having.

Really? I doubt that very much. I think OP is having very general and
basic problems many of which would be answered by such a book. In fact,
I suspect the OP would be thrilled with a simple well-commented example
of using the library in a short C program.

> What's lacking is any indication of what the OP wants to do, and what part
> of it isn't working or making sense.

I feel I got a sense of that -- they were at sea with whole thing.
That's one reason, I suspect, they could not be more specific about what
was not working. I may be wrong, but asking "what do you want to do?"
when the answer seems to be "to learn how to use this library" is
unlikely to get a lot more detail out of a beginner.

> We don't know whether the OP has read
> the documentation (although I'm guessing not), or ever reads documentation,
> or actually knows what a regex is, or is on a platform where these functions
> are not a part of the system library, or anything.

Indeed. Note that would not (and did not) comment on any of these being
asked.

> No place to start.

That's one way of looking at it. My view is that there lots of places
to start.

--
Ben.