943,801 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 881
  • C++ RSS
May 29th, 2009
0

Regex - AND operator

Expand Post »
hi ,
can anyone tell how to define AND operator by regular expression.

Actually I want to return match if and only if 2 substrings will appear in string.

For example i have string "Successful Logon: User Name: Administrator Domain: Logon ID: (0x0,0x154CB759) Logon Type:2 Logon Process: Advapi Authentication Package: MICROSOFT_AUTHENTICATION_PACKAGE_V1_0 Workstation Name: LC2KEDS1"

and match should be return only for Administrator and Logon Type:2 if anyone of this found match should not return
it only return when both will be there
Reputation Points: 59
Solved Threads: 10
Junior Poster
ashishchoure is offline Offline
103 posts
since Sep 2008
May 29th, 2009
0

Re: Regex - AND operator

Yeah that's pretty simple, you could just use string find twice.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6. std::string t = "Administrator blah blah logon:2 blah blah";
  7.  
  8. int matchOne = t.find("Administrator");
  9. int matchTwo = t.find("logon:2");
  10.  
  11. //std::cout << matchOne << std::endl;
  12. //std::cout << matchTwo;
  13.  
  14. if (( matchOne >= 0 ) && ( matchTwo >= 0 ))
  15. {
  16. std::cout<<"Found";
  17. }
  18.  
  19. std::cin.get();
  20. }
Last edited by iamthwee; May 29th, 2009 at 4:57 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 29th, 2009
0

Re: Regex - AND operator

Something like:

^.*Administrator.*Logon Type:2.*$

should do the job.
Last edited by jencas; May 29th, 2009 at 4:42 am. Reason: typo correction
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
May 29th, 2009
0

Re: Regex - AND operator

Click to Expand / Collapse  Quote originally posted by jencas ...
Something like:

^.*Administrator.*Logon Type:2.*$

should do the job.

this expression selects all words between administrator and Logon Type:2
Reputation Points: 59
Solved Threads: 10
Junior Poster
ashishchoure is offline Offline
103 posts
since Sep 2008
May 29th, 2009
0

Re: Regex - AND operator

Regex doesn't exist in c++.

Unless you're using boost libraries or C++/cli/.net, you already have a solution post #2. There is no need to use regex here. If you need to use it, please give us a reason why.
Last edited by iamthwee; May 29th, 2009 at 5:43 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 29th, 2009
0

Re: Regex - AND operator

Click to Expand / Collapse  Quote originally posted by iamthwee ...
Regex doesn't exist in c++.

Unless you're using boost libraries or C++/cli/.net, you already have a solution post #2. There is no need to use regex here. If you need to use it, please give us a reason why.

I am using PCRE library for using regular expression in c++ application.
second solution is fine but doing 2 findings and finally apply && operator in if condition will increase overhead.

I want this as a single regular expression and after APIs of PCRE library will do the stuff. PCRE (Perl Compatible Regular Expressions) is an open source library written in C that allows developers to add regular expression support in c/c++ application
Reputation Points: 59
Solved Threads: 10
Junior Poster
ashishchoure is offline Offline
103 posts
since Sep 2008
May 29th, 2009
0

Re: Regex - AND operator

You still haven't answered why post #2 isn't an adequate solution though. Using regex is far more expensive in terms of overhead so your argument is BS.
Last edited by iamthwee; May 29th, 2009 at 6:15 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 29th, 2009
0

Re: Regex - AND operator

Click to Expand / Collapse  Quote originally posted by iamthwee ...
You still haven't answered why post #2 isn't an adequate solution though.

second solution is fine but doing 2 findings and finally apply && operator in if condition will increase overhead.
Reputation Points: 59
Solved Threads: 10
Junior Poster
ashishchoure is offline Offline
103 posts
since Sep 2008
May 29th, 2009
1

Re: Regex - AND operator

Click to Expand / Collapse  Quote originally posted by iamthwee ...
There is no need to use regex here. If you need to use it, please give us a reason why.
Reread the article of the TO carefully.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
May 29th, 2009
0

Re: Regex - AND operator

That's a well-known identity:
C++ Syntax (Toggle Plain Text)
  1. A and B <=> not (not A or not B)
As far as I know there are not and or operators in all versions of regular expressions...

Remark on post#2: string::find returns unsigned value. It's a bad practice to assign it to int variables then test if it's less than 0 (see std::basic_string::npos constant value)...
None the less it looks like the fastest solution in C++
Last edited by ArkM; May 29th, 2009 at 8:37 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: drawing a line in visual c++v6.0
Next Thread in C++ Forum Timeline: how to convert text fine to bin file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC