-1

I have a value like this:

Supoose I have a string:

s = "server ('m1.labs.teradata.com') username ('u\'se)r_*5') password('uer 5')  dbname ('default')";

I need to extract

  • token1 : 'm1.labs.teradata.com'
  • token2 : 'u\'se)r_*5'
  • token3 : 'uer 5'

I am using the following regex in cpp:

regex re("(\'[!-~]+\')"); 

sregex_token_iterator i(s.begin(), s.end(), re, 0);
sregex_token_iterator j;

unsigned count = 0;
while(i != j)
  {
    cout << "the token is"<<"   "<<*i++<< endl;
    count++;
  }
cout << "There were " << count << " tokens found." << endl;

return 0;
PeskyPotato
  • 670
  • 8
  • 20
hydra123
  • 337
  • 5
  • 14
  • 1
    Simpliest would be `'[^']+'` – Slava Jul 19 '17 at 13:39
  • You need to capture that part, and use `str(1)` to get the capturing group #1 value. – Wiktor Stribiżew Jul 19 '17 at 13:41
  • @Slava: I have a value like this: arg1('FooBar') arg2('Another Value') something else What regex will return the values enclosed in the quotation marks (e.g. FooBar and Another Value)? I am using the following regex in cpp: regex re("(\'[^']+\')") Like this?? – hydra123 Jul 19 '17 at 13:41
  • Supoose I have a string: s = "server ('m1.labs.teradata.com') username ('u\'se)r_*5') password('uer 5') dbname ('default')"; I need to extract token1 : 'm1.labs.teradata.com' token2 : 'u\'se)r_*5' token3 : 'uer 5' – hydra123 Jul 19 '17 at 13:45
  • Do you have to use the `sregex_token_iterator`rather than the regular one? – Galik Jul 19 '17 at 13:45
  • You should put examples in your question that are relevant to your problem. Perhaps edit your question to include your new examples? They turn it into a different question. – Galik Jul 19 '17 at 13:47
  • No I can use anything else , but it should extract the string. – hydra123 Jul 19 '17 at 13:47
  • [Here is your solution](https://ideone.com/IqtxgJ). Here is a [safer variation](https://ideone.com/78WL8W). – Wiktor Stribiżew Jul 19 '17 at 14:03
  • This utterly trivial search doesn't need the complexity of regular expressions. Use `start = std::string::find('\'')` to find the beginning of the target text and then use `end = std::string::find('\'', start)` to find the end. – Pete Becker Jul 19 '17 at 14:07
  • @WiktorStribiżew I want to extract string in the form 'm1.labs.teradata.com' 'u\'se)r_*5' 'uer 5' With the single quotes around it, is their a way? And also my string can contain special character such as "user')5" . With your regex I am not able to do so. I have used the regex as asked in the question but that does not work for me. Please can you help me out with this? And also can you please explain the regex you have written – hydra123 Jul 19 '17 at 15:21
  • [With single quotes](https://ideone.com/g6WA5u) and [with both types of quotes](https://ideone.com/x65hOo). – Wiktor Stribiżew Jul 19 '17 at 15:36
  • @WiktorStribiżew The one with single qoutes one: If I have a string username ('u\\'se)'r_*5'), I want to include the quote which is before r, but the regex is not doing that. Thanks for your time. I really need the help. – hydra123 Jul 19 '17 at 16:18
  • No idea what the rules are now. I cannot help when example strings are inconsistent. Ask Slava, he seems to understand your issue better. – Wiktor Stribiżew Jul 19 '17 at 16:22
  • @WiktorStribiżew Can you help me out with how can I involve a single quotes withing the string which I am extracting? Example : user'5 – hydra123 Jul 19 '17 at 16:24
  • Do you mean `username ('u\\'se)r_*5')` please correct you question or even better provide [mcve] – Slava Jul 19 '17 at 16:52
  • @Slava -: Suppose my string is: "server ('m1.labs.teradata.com') username ('u\'se)r_*5') password('uer 5') dbname ('default')"; Consider the username ('u\'se)r_*5') As you can see I am trying to escape the single quote in " 'u\'se)r_5' " using " \' ", so the output token looks like 'u'se)r_5'. Can You please help me out with the regex for the same. – hydra123 Jul 19 '17 at 18:20

2 Answers2

2

If you do not expect symbol ' inside your string then '[^']+' would match what you need:

regex re("'[^']+'");

live example Result:

the token is   'FooBar'
the token is   'Another Value'
There were 2 tokens found.

if you do not need single quotes to be part of match change code to:

regex re("'([^']+)'");

sregex_token_iterator i(s.begin(), s.end(), re, {1});

another live example

the token is   FooBar
the token is   Another Value
There were 2 tokens found.
Slava
  • 43,454
  • 1
  • 47
  • 90
0

The correct regex for this string would be

(?:'(.+?)(?<!\\)')

https://regex101.com/r/IpzB80/1

Pradeep
  • 3,093
  • 17
  • 21
  • The output string should contain escaped single quote, but the regex givven by you does not work. – hydra123 Jul 19 '17 at 15:29
  • Try this string : It is not working: server ('m1.labs.teradata.com') username ('u\'se)r_*5') password('ue/'r5') dbname ('default') – hydra123 Jul 19 '17 at 16:19
  • @Pradeep C++11 regex does not support look behind - https://stackoverflow.com/questions/14538687/using-regex-lookbehinds-in-c11 – Slava Jul 19 '17 at 16:50