Regular Expression In C++ !!!!.

S

sk.rasheedfarhan

Hi
My problem is, I am running my C++ code [for specifically coded for
Regular expressions ] and it is not giving good results for negation
of a regular expressions.

And my C++ code is Downloaded one here is the URL
http://src.opensolaris.org/source/xref/sfw/usr/src/cmd/less/less-381/regexp.c#regnode

I have checked Negation part related code. But I am not clear with my
problem as well with code. Here are my inputs and results

Regular expression :^[^\n]*?102 (.*?)\n
Regular expression Explanation: Any thing before 102 Other than new
line and after 102 a blank space, any thing minimal matches and new
line followed.

Input:102\b\n
Input explanation : 102 blankspace and Newline.

Result :False.


But for this input, it has give the Result as True. But it given wrong
Result.

I check the same thing with this URL http://regexlib.com/RETester.aspx


So please can any body sort out where is the problem in the above code
or can any one give me the best code which work for universal Regular
expression format.

Regards,
Rs.
 
P

Pete Becker

Regular expression :^[^\n]*?102 (.*?)\n
Regular expression Explanation: Any thing before 102 Other than new
line and after 102 a blank space, any thing minimal matches and new
line followed.

Input:102\b\n
Input explanation : 102 blankspace and Newline.

Result :False.

The regular expression you've used is more complicated than it needs to
be. The initial ^ and the ?'s aren't needed. I'd write it like as
'[^\n]*102 (.*)\n' (ignore the single quotes, of course). Also, unless
you've used unusual options in the regular expression search, a . is the
same as [^\n], so you can write this as '.*102 (.*)\n'. To write that as
a string literal in C++ you need double quotes and double backslashes:
".*102 (.*)\\n".

It's much simpler to read a line at a time, and check whether the line
matches your expression. That way you don't have to deal with newlines,
which are a bit tricky in regular expressions. If you do that, the
regular expression becomes '.*102 (.*)'. Remember, the * is greedy, so
..* will match everything out to the end of the line.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
A

Anand Hariharan

Regular expression :^[^\n]*?102 (.*?)\n
Regular expression Explanation: Any thing before 102 Other than new
line and after 102 a blank space, any thing minimal matches and new
line followed.
Input:102\b\n
Input explanation : 102 blankspace and Newline.
Result :False.

The regular expression you've used is more complicated than it needs to
be. The initial ^ and the ?'s aren't needed. I'd write it like as
'[^\n]*102 (.*)\n' (ignore the single quotes, of course). Also, unless
you've used unusual options in the regular expression search, a . is the
same as [^\n], so you can write this as '.*102 (.*)\n'. To write that as
a string literal in C++ you need double quotes and double backslashes:
".*102 (.*)\\n".

It's much simpler to read a line at a time, and check whether the line
matches your expression. That way you don't have to deal with newlines,
which are a bit tricky in regular expressions. If you do that, the
regular expression becomes '.*102 (.*)'. Remember, the * is greedy, so
.* will match everything out to the end of the line.

.... which in turn reduces the RE to '102 '. Based on provided example
alone, the OP has no need for REs.

- Anand
 
P

Pete Becker

Anand said:
... which in turn reduces the RE to '102 '.

It depends on whether the search is for a complete match or a partial
match. And either way, this drops the capture group at the end, which
is, presumably, the point of the match.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

It depends on whether the search is for a complete match or a partial
match. And either way, this drops the capture group at the end, which
is, presumably, the point of the match.

Yes, but it would probably be as easy to do this in standard C++ and
probably just as efficient (if not more so). In pseudo-code something
like this:

while (not EOF) // Assuming reading from file
{
if (readLine.substr(0,4) == "102 " && readLine.size() > 4)
{
// A match
}
// read next line
}
 
P

Pete Becker

Erik said:
Yes, but it would probably be as easy to do this in standard C++ and
probably just as efficient (if not more so). In pseudo-code something
like this:

while (not EOF) // Assuming reading from file
{
if (readLine.substr(0,4) == "102 " && readLine.size() > 4)
{
// A match
}
// read next line
}

Nope, that only matches if "102 " appears at the beginning of the string.

Obviously, for any particular pattern, you can hard-code a search that
does the same thing as the regular expression. What you gain from
regular expressions is simplicity, clarity, and ease of maintenance. If
you later found you had to match "112 " as well as "102 ", changing the
regular expression is much easier than changing the hard-coded search.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
A

Anand Hariharan

It depends on whether the search is for a complete match or a partial
match. And either way, this drops the capture group at the end, which
is, presumably, the point of the match.

I missed the group at the end.

Basically the intent of the RE is to get the text immediately
following the /last/ occurrence of '102 ' in a line, up to the end of
that line. This /can/ be done without using TR1.

Note that I write this after having seen your other post in this
thread (MID:[email protected]). Whether
REs increase clarity is debateable. REs are noisy enough to begin
with. A language that requires every backslash to be escaped with
another backslash, does not exactly help.

- Anand
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top