how to use regex library

S

Seebs

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

Exactly. However, it's as good an answer as any to someone who picked
the library at random without any notion of what it would do or why. I
mean, it "uses" the library.

-s
 
I

Ike Naar

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.

It occurs to me that the OP meant to say that the library was
selected randomly from a set of libraries that offer regex support,
not from the set of all libraries written in C.
 
M

Mark Bluemel

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

The manual explains what it does, the test program (try.c) and the
driver for the test program (the "tests" file) show how to use it. If
you understand regular expressions, it should be fairly straightforward
to understand.

Try spending more time looking at the information you have rather than
asking other people to do your thinking for you.
 
J

James Kuyper

It occurs to me that the OP meant to say that the library was
selected randomly from a set of libraries that offer regex support,
not from the set of all libraries written in C.

Given that this was his approach, the right answer to the
correctly-posed question might be "don't use this library, use this
alternative library...". However, the question was not correctly posed.
It takes more information, of a different type, than has been provided
to decide whether this is the right answer, and which alternative he
should be redirected to if it is.
 
B

Ben Bacarisse

Seebs said:
Exactly. However, it's as good an answer as any to someone who picked
the library at random without any notion of what it would do or why. I
mean, it "uses" the library.

You are 100% correct. I hope that is enough. CS types seem to be more
likely than almost any other to answer "do you want red or white?" with
"yes" and "how do I use a screwdriver" with "you put it in your mug and
move it in circles". It makes one want to hang out with jazz musicians.
 
O

Owner

Try spending more time looking at the information you have rather than
asking other people to do your thinking for you.

Sorry to bother you again.

Now I get it some of it. My mind was stick to the getting a matched

position and length, I didn't see that getting those information is

irrelevant. And the library provides mainly whether there is

a match, and routine for replacing matched string.

I knew what regexp was(that's why I wanted use the library).

I just didn't understand the process (sequence) of using

regexp library. I was able to compile try.c and own simple

test routine.

I did get print out of matched string with following code.

while (r->startp[0] < r->endp[0])
putchar(*r->startp[0]++);
putchar('\n');

and Now I understand better, I copied following routine

regsub(r, argv[3] , result);

printf("%s\n", result);

to my test routine.

getting output what I expected.

c:\Users\(no showing)\Desktop\regexp.old_project>retest " hi.*" "this is
hi to the worl rd" "& well &"
1 hi to the worlrd well hi to the worlrd

1 is just indicating a match was found.

I thank all for your help. Now I can sleep well

Thank you again
 
S

Seebs

You are 100% correct. I hope that is enough. CS types seem to be more
likely than almost any other to answer "do you want red or white?" with
"yes" and "how do I use a screwdriver" with "you put it in your mug and
move it in circles". It makes one want to hang out with jazz musicians.

Heh.

It does serve a purpose, though. It's there as a way of indicating that
the problem specification is crap.

There's a famous observation that it is very hard to get people to answer
a question, but it's easy to get them to correct a wrong answer. Thus, if
you really need experts to tell you whether the language spec, or UNIX
environment, provides a clear semantic sense of exit(3), asking is only
sometimes going to get a result, but posting "As we all know, exit(3)
is only supposed to be used in government projects to indicate functionality
which will be added in the following fiscal year, but I was thinking of
writing a program which exits with..." will almost certainly get a
detailed rebuttal.

Similarly, no amount of "but I still don't know what you want, could you
explain what you want" can get through to some people, but many of those
people are VERY capable of correcting something that isn't what they want
with a detailed specification. It's just that for some reason, they can't
comprehend "I don't know what you want to do" until it's phrased as "oh,
I see, you want to cuddle a flamingo".

-s
 
S

Seebs

It occurs to me that the OP meant to say that the library was
selected randomly from a set of libraries that offer regex support,
not from the set of all libraries written in C.

Could be. The statement made, though, was merely that the library was
not chosen for any specific reason.

I can't tell how much of this is vague thinking, and how much of it is
just vague writing.

-s
 
K

Keith Thompson

Owner said:
Sorry to bother you again.

Now I get it some of it. My mind was stick to the getting a matched

position and length, I didn't see that getting those information is

irrelevant. And the library provides mainly whether there is

a match, and routine for replacing matched string.
[...]

Please don't double-space your text like this. It just makes it more
difficult to read. Use paragraphs to separate paragraphs, not lines.

See the vast majority of articles in this newsgroup for examples.
 
S

Seebs

In short, these are the kinds of questions you are prepared to answer.
However this is of no help to the OP, who is obviously muddling around
at the very beginning of understanding how to the said package.

The missing verb pretty much highlights the problem I'm having; I can
speculate that you probably meant "how to use", but I don't have any way
of telling whether that's reasonable.
Of course there is. Below I quote Mark Bluemel's response. It is an
excellent example of how to answer such a query. He was helpful. You
are not.

Right. It's one of the known limitations of the way I respond to questions;
I can only answer the question actually asked, I can't mind-read. To this
day, I still don't know whether the OP actually specifically needed to use
regexes, or was just trying to experiment with using libraries in the general
case. I can't tell whether he selected Spencer's regex library randomly from
the pool of all libraries, or as a regex library (a much narrower field). I
can't tell whether he wants regular expressions or whether all he really
needs is someone to point out that strstr() is in the standard library. I
have no clue what he wants to *do*.

If the OP had said, say:
I want to match strings against a regex, but I don't know how to use
this API to accomplish that. The description of the arguments
used by regexec() doesn't make any sense to me.
that would be something I could respond to easily, because it would give me
some idea of what the question was.

That said, I'm not at all sure I accept the claim that my response isn't also
helpful. The OP would get much better answers much sooner if he would learn
to ask specific questions and explain what he's trying to do. Pointing out
to him that not everyone is telepathic might be one way to help him develop
this valuable skill.

In the long run, no question anyone has ever answered for me has helped me as
much as being told how to ask better questions.

-s
 
G

Geoff

Nothing of value.


I'm laughing my ass off here as you guys have wasted more time and
bandwidth discussing why the OP's question was malformed than you
would have wasted answering an incorrect speculation about what the
OP's question was. If you don't understand and don't want to try to
understand the OP's question then it would have been far more
courteous and productive to have ignored the post. The rest is just
mental masturbation.
 
S

Shao Miller

Reminds me of a recent e-mail exchange between my wife and someone who
posted a "wanted" notice for a "replacement hard drive". She explained
that she needed more information -- IDE or SATA, what capacity, etc. It
went back and forth several times, without any useful information from
him, even after "here's how you tell IDE from SATA"-like details. (We
had several old drives of both types laying around, and were willing to
give one to him.) It soon became obvious that he had no business opening
up a computer and replacing a hard drive, due to his total lack of
knowledge and ability to get a clue, not to mention his increasing
attitude of "why are you making this so difficult" answers.

Finally, we were able to determine that he was asking the wrong question
from the get-go, when he said something about just wanting to connect
his "Dell monitor" to a "new hard drive". I assume that everyone here
can fill in the rest.

This sounds like every "Quick question for you..." phone call I get.
Very familiar. :)
 
S

Seebs

Nothing of value.

Wow, @invalid.invalid dislikes my posts! How new and refreshing!
mental masturbation.

Elegant summary.

The funny part is that you took the time to write an insulting post about
how you think other peoples' posts aren't valuable.

I'm here recreationally. Calling anything I do here a "waste of time" is
sort of missing the point. I enjoy the conversations, I sometimes learn
stuff about C, and who knows, maybe the OP will ask a more specific question
some time.

-s
 
G

Geoff

Wow, @invalid.invalid dislikes my posts! How new and refreshing!


Elegant summary.

The funny part is that you took the time to write an insulting post about
how you think other peoples' posts aren't valuable.

I'm here recreationally. Calling anything I do here a "waste of time" is
sort of missing the point. I enjoy the conversations, I sometimes learn
stuff about C, and who knows, maybe the OP will ask a more specific question
some time.

-s

Considering that the OP posted from your favorite troll source AIOE,
you have a strange sense of recreation.
 
O

Owner

I got one more problem. I dont' think Man page mentioned
about second matching on the same string, when I read it.
I tried call "i = regexec(r, argv[2]);" twice, but didn't move
forward after first match. I did found however
"i = regexec(r, r->endp[0]);" matches the second substring.
but I don't think it's meant to be used this way.
How do you match same pattern more than twice on the same line?
 
S

Seebs

I got one more problem. I dont' think Man page mentioned
about second matching on the same string, when I read it.
I tried call "i = regexec(r, argv[2]);" twice, but didn't move
forward after first match. I did found however
"i = regexec(r, r->endp[0]);" matches the second substring.
but I don't think it's meant to be used this way.
How do you match same pattern more than twice on the same line?

Well, typically, you would find a point in the line after the first
match, and use regexec to match there.

Why don't you think it's meant to be used this way? What seems unusual
to you about using the endpoint of a previous match as the starting place
to look to see whether there are any more matches? Why would you think
that it would "move forward" after the first match?

Let's translate this all into plain language.

Consider this string. We need a name for it, let's call it "Bob".
"hello, world, hello, dolly, hello, sailor".

Now, I ask you: Does Bob contain the word hello? If so, where?

You respond with something like:
Yes: ">hello<, world, hello, dolly, hello, sailor".

Okay. Now I ask you: Does Bob contain the word hello? If so, where?

Why would your answer be different? I didn't ask you a different
question. Nothing changed. The string isn't different from what it
used to be. It would be ridiculous for your answer to be different.

Now, imagine instead that I asked you "Does Bob contain the word hello
somewhere after the 5th character? If so, where?", then you might
respond:
Yes: ", world, >hello<, dolly, hello, sailor".

Computers do exactly what you ask them to. They do not do what you
meant, and they don't care whether you just asked them that two seconds
ago; they're just going to keep answering the question you asked.

-s
 
T

Tim Rentsch

Seebs said:
In short, these are the kinds of questions you are prepared to answer.
However this is of no help to the OP, who is obviously muddling around
at the very beginning of understanding how to the said package.

The missing verb pretty much highlights the problem I'm having; I can
speculate that you probably meant "how to use", but I don't have any way
of telling whether that's reasonable.
Of course there is. Below I quote Mark Bluemel's response. It is an
excellent example of how to answer such a query. He was helpful. You
are not.

Right. It's one of the known limitations of the way I respond to questions;
I can only answer the question actually asked, I can't mind-read.
[snip elaboration]

Since it's your limitation, and since you know about it,
maybe it's better for you not to respond in such cases,
and let other people answer who are more able to help.
Yes? I understand your frustration at not being able
to provide a helpful answer, but the OP isn't responsible
for that frustation, even if his posting is part of what
caused it.
 
S

Seebs

Because that is a quite reasonable expectation.

Is it?

I can certainly see it being one of the possible outcomes, but I wouldn't
*expect* it.
Many search
facilities operate either operate that way or they have a 'next
instance' command.

This is true, but some don't. And I guess that's the thing; I might
anticipate the possibility of an interface doing something, but if I
test it and it doesn't, that's not usually going to be a major source
of confusion.

In C, if I write sqrt(1), I don't expect to get 1 the first time I call
it and -1 the second time I call it. In general, unless an interface
is specified otherwise, I expect it to behave the same way every time,
and nearly all the interfaces that are specified otherwise, we avoid
like the plague because they're bugs waiting to happen.

*cough*strtok*cough*.
It is misleading to say that computers 'do what
you tell them to do'. Unless you are programming down to the bare
iron you don't tell computers what to do - you tell some piece of
software what to do. What that software does is (sometimes) what the
programmer who wrote it decided it ought to do. It is a serious
mistake to blame the computer for the design errors of the software
creators.

True enough, but in C, you generally have a pretty good bet that something
will do what it says it does. If it doesn't say "repeated calls with the
same arguments will do different things", then they probably won't. :)

-s
 
S

Seebs

Since it's your limitation, and since you know about it,
maybe it's better for you not to respond in such cases,
and let other people answer who are more able to help.
Yes?

Maybe. The thing is, in a fair number of cases, other people can't
tell; this is why ESR's "smart questions" page exists. Perhaps more
importantly, in some cases, other people can't tell that they can't
tell, and no one gets anywhere until someone points out that the question
was vague.
I understand your frustration at not being able
to provide a helpful answer, but the OP isn't responsible
for that frustation, even if his posting is part of what
caused it.

Having read a number of his posts, I would tend to consider him responsible
for his failure to clearly state what he's trying to do, because he clearly
has good enough English to read ESR's page and follow the guidelines there.
His questions strike me as not showing much of an effort to figure things
out before he posts, and I like to remind people that even a small amount
of effort can save a LOT of time.

-s
 
S

Seebs

I wouldn't expect that you would expect it. But that doesn't mean it
isn't reasonable.

I tend to feel that forming a definite expectation is counterproductive
in the absence of clear data.
I would have thought you would have read the documentation first - I
may be wrong but that seems like a reasonable expectation to me.

Well, I'm assuming for the sake of argument that the documentation is
unclear on the issue, because otherwise why would I get to the point of
posting on Usenet about it?
Sqrt is a search function?

No, but it's a function which tells me something about its argument.
I'm with you. I expect fgets to return the same line each time I call
it. :)
More seriously, there is a difference between "returns the same
result" and "behaves the same way".

Yes.

But fundamentally, asking whether a string matches a pattern does not
consume the string. Asking for the next line of input consumes input.

I wouldn't expect a regex function to consume the string, because the
string is const-qualified.
Except when they do. :)

Sure. But generally there's a clear reason for which that has to happen,
or it's explicit that that's what happens.

-s
 

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
474,091
Messages
2,570,605
Members
47,225
Latest member
DarrinWhit

Latest Threads

Top