Regex - AND operator

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 46
Reputation: ashishchoure is on a distinguished road 
Solved Threads: 3
ashishchoure's Avatar
ashishchoure ashishchoure is offline Offline
Light Poster

Regex - AND operator

 
0
  #1
May 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,275
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Regex - AND operator

 
0
  #2
May 29th, 2009
Yeah that's pretty simple, you could just use string find twice.

  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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Regex - AND operator

 
0
  #3
May 29th, 2009
Something like:

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

should do the job.
Last edited by jencas; May 29th, 2009 at 4:42 am. Reason: typo correction
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 46
Reputation: ashishchoure is on a distinguished road 
Solved Threads: 3
ashishchoure's Avatar
ashishchoure ashishchoure is offline Offline
Light Poster

Re: Regex - AND operator

 
0
  #4
May 29th, 2009
Originally Posted by jencas View Post
Something like:

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

should do the job.

this expression selects all words between administrator and Logon Type:2
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,275
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Regex - AND operator

 
0
  #5
May 29th, 2009
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 46
Reputation: ashishchoure is on a distinguished road 
Solved Threads: 3
ashishchoure's Avatar
ashishchoure ashishchoure is offline Offline
Light Poster

Re: Regex - AND operator

 
0
  #6
May 29th, 2009
Originally Posted by iamthwee View Post
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,275
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Regex - AND operator

 
0
  #7
May 29th, 2009
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 46
Reputation: ashishchoure is on a distinguished road 
Solved Threads: 3
ashishchoure's Avatar
ashishchoure ashishchoure is offline Offline
Light Poster

Re: Regex - AND operator

 
0
  #8
May 29th, 2009
Originally Posted by iamthwee View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Regex - AND operator

 
1
  #9
May 29th, 2009
Originally Posted by iamthwee View Post
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.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Regex - AND operator

 
0
  #10
May 29th, 2009
That's a well-known identity:
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 519 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC