extracting whitespaces using regex in cpp
Asked Answered
S

0

0

I have the following string :

s = "server ('m1.labs.terada')ta.com') username ('user5') password('use r5') dbname ('default')";

I have defined a regex for extracting the values between the paranthesis,i.e m1.labs.terada')ta.com , user5.

regex re("\(\'[!-~]+\'\)");
sregex_token_iterator i(s.begin(), s.end(), re, 1);
sregex_token_iterator j;

However, I am not able to extract 'use r5'. Is there any way I can modify the regex to include white spaces as well?

Scum answered 19/7, 2017 at 4:16 Comment(10)
Well, a space character is not between a ! and a ~. Your regular expression deliberately excludes it. Perhaps it shouldn't.Nubbly
Okay. If i use the regex such as : regex re("(\'[!-~]+\x20*\')") . How can I match strings like ' user 5 user'??Scum
Your input string is not parseable using standard means. Are you sure the quote inside a string isn't supposed to be quoted?Pickax
@RustyX Okay let the string be server ('m1.labs.teradata.com') username ('u5') password('uer 5') dbname ('default') How shall I parse this in cpp boost?Scum
Your problem is this: if a token is allowed to contain spaces and parentheses and apostrophes, then why isn't m1.labs.terada')ta.com') username ('user5') password('use r5') dbname ('default one big token? In other words, your grammar is ambiguous. You first need to come up with a precise definition of "token" that excludes the above, but still includes m1.labs.terada')ta.com and use r5. Spell this out in plain English, and perhaps we'd be able to come up with a regex that formalizes this description.Nubbly
@IgorTandetnik Yes, I am getting this token : m1.labs.terada')ta.com') username ('user5') password('use r5') dbname ('default as the output. However, I want the tokens as: token 1: 'm1.labs.terada')ta.com' token 2: 'user5' token 3 : 'use r5'Scum
Again - explain to me, in plain English, why m1.labs.terada')ta.com') username ('user5') password('use r5') dbname ('default should not be considered a valid token. What rules on token syntax does it violate? Just saying "I want this" is not enough; DWIM system has yet to be perfected.Nubbly
It does not violate any rule. I am confused as what to set the delimiter, so that token extracted is between " (' " " ') "Scum
m1.labs.terada')ta.com') username ('user5') password('use r5') dbname ('default is in fact enclosed in (' and '). If that's the rule, then it looks like you got the correct answer. If you don't consider this to be the correct answer, you need to figure out what's wrong with it, and formulate a different rule or set of rules.Nubbly
I am not able to do that, I am only able to extract string between the quotes. but when there is space in the sting , it is causing a problm.Scum

© 2022 - 2024 — McMap. All rights reserved.