how to use regex library

O

Owner

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
 
S

Seebs

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
 
O

Owner

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
 
J

James Kuyper

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.
 
M

Mark Bluemel

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 != NULL && r->endp != NULL)
printf(" \\%d", i);
 
J

Jorgen Grahn

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
 
S

Seebs

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-questions.html

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
 
B

Ben Bacarisse

James Kuyper said:
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?".
 
S

Seebs

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
 
B

Ben Bacarisse

Seebs said:
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.
 
J

James Kuyper

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?".

There are two things wrong with that question:
1. It's too vague. Seebs has already expounded at length on this issue,
so I don't need to add anything more.
2. The XYZ library might be the wrong solution to the OP's question.
 
S

Seebs

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.

Could be.
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.

Well, maybe. Except...

Use it *to do what*? To prop up a couch with one leg too short?

I mean, something as basic as "I have an array of strings and want to see
whether any of them match a regex" would help immensely.
That's one way of looking at it. My view is that there lots of places
to start.

Well, yeah. But too many places to start has the same effect as none; I
can't actually start in all of them, and if I try to answer all the possible
questions, I spend hours and hours on something that would take thirty
seconds if the OP were just a LITTLE more forthcoming.

WHY does the OP want to use this library, as opposed to some other library?
Was it just a random selection of a library written in C? Was there a
specific need to use regexes? Is the OP on a platform that doesn't have
regex support already in the system libraries?

-s
 
O

Owner

Could be.

Can I say something? The good examples will help a lot

using library. Yes, I'm thrilled just to think about it.

Because I can't sleep well sometimes just thinking of the problems

Well, maybe. Except...

Use it *to do what*? To prop up a couch with one leg too short?

I mean, something as basic as "I have an array of strings and want to see
whether any of them match a regex" would help immensely.


Well, yeah. But too many places to start has the same effect as none; I
can't actually start in all of them, and if I try to answer all the possible
questions, I spend hours and hours on something that would take thirty
seconds if the OP were just a LITTLE more forthcoming.

WHY does the OP want to use this library, as opposed to some other library?
Was it just a random selection of a library written in C? Was there a
specific need to use regexes? Is the OP on a platform that doesn't have
regex support already in the system libraries?

-s

it was library I found randomly. and any library easier to use would help

me. And I think win 7 doesn't support regex.
 
O

Owner

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 != NULL && r->endp != NULL)
printf(" \\%d", i);


I ended up back to the beginning. I read man after converting

the regexp.3 file to ps using groff and again converted it to

pdf format file. r.startp is pointer to start of matched string

and endp is a pointer to the one after matched string.

what does regsub do? it says it's for substitution.

can I build sed program with regsub?

void
regsub(rp, source, dest)
const regexp *rp;
const char *source;
char *dest;
{
 
S

Seebs

Can I say something? The good examples will help a lot
using library. Yes, I'm thrilled just to think about it.
Because I can't sleep well sometimes just thinking of the problems

But you still can't say what you want to use it for. Okay, fine.

#include "regex.h"
int use_regex_library() {
int (*comp)(regex_t *, const char *, int);
int (*exc)(const regex_t *, const char *, size_t, regmatch_t [], int);
size_t (*error)(const regex_t *, char *, size_t);
void (*fr)(regex_t *);
comp = regcomp;
exc = regexec;
error = regerror;
fr = regfree;

return 0;
}
/* not enough... hmm. */

/* okay, how about we use regcomp... AS A SENTINEL VALUE!
*/
typedef int (*comper)(regex_t *, const char *, int);
comper foo[] = {
NULL,
regcomp
};

int use_it_more(void) {
comper bar;
int i;
for (i = 0; foo != regcomp; ++i) {
/* do something awesome! */
}
}
it was library I found randomly. and any library easier to use would help
me. And I think win 7 doesn't support regex.

See, this is what I was afraid of.

You don't even want to use regular expressions. You just want to "use a
library".

Okay, here's a library:

/* foo.c */
int easy_to_use(void) { return 3; }

To use this, just link it with your code, and call easy_to_use() any time
you need the number 3.

Which is to say... It seems as though your problem has NOTHING AT ALL to do
with the regex library. You don't actually need to use regular expressions,
you don't care what the library does.

My guess is that your ACTUAL question is something like "how do I link with
another library".

But until you can come up with some kind of specific thing you want to do,
it's very hard for us to give you an informative answer.

You know that link I gave you? READ IT.

We can do all sorts of things for you, but if you don't know what you want,
you can hardly expect us to guess.

I suggest you start by picking a thing you want to do, then trying to figure
out how to do it. "Use a library" is not a goal; it's a means to achieving
a goal. Start with an actual goal.

-s
 
B

Ben Bacarisse

James Kuyper said:
There are two things wrong with that question:
1. It's too vague. Seebs has already expounded at length on this issue,
so I don't need to add anything more.
2. The XYZ library might be the wrong solution to the OP's question.

I'm sorry you don't want to expand on 1 because I think it's key. Some
people have vague questions because they don't know enough to be more
specific. I got a clear sense of what the OP wanted and the fact that
they seem to be happy with the reply from... sorry can't find it right
now, backs this up. With some very vague questions, almost any help is
gratefully received because it blows away some of the fog. I don't have
the same problem with them that some people seem to have.

I find 2 very odd. The library may be wrong but learning about it might
be good. Some people go so far as to claim that all knowledge is of
some benefit. I get that you may not want to answer unless the
questioner has demonstrated that the information won't lead to the wrong
solution, but that approach is far too authoritarian for my liking.
 
B

Ben Bacarisse

Seebs said:
Could be.


Well, maybe. Except...

Use it *to do what*? To prop up a couch with one leg too short?

I mean, something as basic as "I have an array of strings and want to see
whether any of them match a regex" would help immensely.

You are easily pleased, in the sense that I don't think that makes much
difference. I simply added that (or something very similar) myself and
though it might be wrong (the op might indeed be fixing the furniture)
the result would have been some information about using that library
(and some advice about alternatives). I did not have time because
someone already did that. I don't worry that the OP's couch might
remain unfixed -- they will have learnt something, and they will ask a
better question about couch repair later once the realise that the regex
library won't help.
Well, yeah. But too many places to start has the same effect as none; I
can't actually start in all of them, and if I try to answer all the possible
questions, I spend hours and hours on something that would take thirty
seconds if the OP were just a LITTLE more forthcoming.

WHY does the OP want to use this library, as opposed to some other library?
Was it just a random selection of a library written in C? Was there a
specific need to use regexes? Is the OP on a platform that doesn't have
regex support already in the system libraries?

I am not plagued by these worries. I think the OP is learning, not
building a bridge that you are going to drive over. I suppose my
surprise is that you want to know the answer to these questions. I'd
likely have asked some of them, had I been in time to answer, but I
don't *need* to know the answers before offering something.
 
S

Seebs

I'm sorry you don't want to expand on 1 because I think it's key. Some
people have vague questions because they don't know enough to be more
specific. I got a clear sense of what the OP wanted and the fact that
they seem to be happy with the reply from... sorry can't find it right
now, backs this up.

Really?

I'd say the post saying that the library was selected randomly sort of
indicates that the question is... well, I still can't tell what it is.

-s
 
B

Ben Bacarisse

Seebs said:
Can I say something? The good examples will help a lot
using library. Yes, I'm thrilled just to think about it.
Because I can't sleep well sometimes just thinking of the problems

But you still can't say what you want to use it for. Okay, fine.

#include "regex.h"
int use_regex_library() {
int (*comp)(regex_t *, const char *, int);
int (*exc)(const regex_t *, const char *, size_t, regmatch_t [], int);
size_t (*error)(const regex_t *, char *, size_t);
void (*fr)(regex_t *);
comp = regcomp;
exc = regexec;
error = regerror;
fr = regfree;

return 0;
}
/* not enough... hmm. */

/* okay, how about we use regcomp... AS A SENTINEL VALUE!
*/
typedef int (*comper)(regex_t *, const char *, int);
comper foo[] = {
NULL,
regcomp
};

int use_it_more(void) {
comper bar;
int i;
for (i = 0; foo != regcomp; ++i) {
/* do something awesome! */
}
}


To the OP: I'd characterise this as sarcastic code -- please don't take
it seriously.

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,952
Messages
2,570,111
Members
46,692
Latest member
NewtonChri

Latest Threads

Top