Hello,
I need to extract words from a wxString but with special case for the " and treating the content between two " as a word.
for example the wxString "One Two "Three Four" Five" should be converted into :
One
Two
Three Four
Five
Ive tried with wxStringTokenizer without success.
Could you please let me know if there is an obvious way to do it ?
Thanks in advance
how to extract words ? Topic is solved
how to extract words ?
Last edited by anonbeat on Wed Nov 12, 2008 7:26 am, edited 1 time in total.
For now I have this code that solves what I need.
Code: Select all
wxArrayString Words;
wxString SearchStr = SearchTextCtrl->GetLineText( 0 );
wxString ResStr;
size_t index, len;
wxRegEx RegEx( wxT( " *([^ ]*|\\" *[^\\"]* *\\") *" ) );
while( SearchStr.Length() && RegEx.Matches( SearchStr ) )
{
RegEx.GetMatch( &index, &len );
Words.Add( RegEx.GetMatch( SearchStr, 1 ) );
SearchStr = SearchStr.Mid( len );
}
Take a look at http://docs.wxwidgets.org/stable/wx_wxs ... gtokenizer
you should use wxStringTokenizer to tokenize the strings based on your choice of delimiter.
Code: Select all
wxStringTokenizer tkz(wxT("first:second:third:fourth"), wxT(":"));
while ( tkz.HasMoreTokens() )
{
wxString token = tkz.GetNextToken();
// process token here
}
What I need is extract search words from a input text control. The user can type a single word, some words or some words with some of them enclosed with " chars.
If enclosed with " chars the words will be treated like a literal and should be searched as it is even if it has spaces in it.
So the separator is the space but spaces are allowed if enclosed with ".
Please if you know how to do it better than I did it let me know
Thanks in advance
If enclosed with " chars the words will be treated like a literal and should be searched as it is even if it has spaces in it.
So the separator is the space but spaces are allowed if enclosed with ".
Please if you know how to do it better than I did it let me know
Thanks in advance
I think using wxRegExp is a bit too much for this.
You could just for-loop through all characters in the string. Add characters to a temporary string until you get a seperator (space or quote). Then store the temporary string as a search term. Keep a boolean to indicate if you are inside quotes or not. If you are, spaces shouldn't count as seperators. Continue until the end of the input string.
Note that continuously adding characters to a string is not very effecient so maybe use wxStringBuffer for better performance. Or preallocate a large enough buffer with wxString::Alloc().
You could just for-loop through all characters in the string. Add characters to a temporary string until you get a seperator (space or quote). Then store the temporary string as a search term. Keep a boolean to indicate if you are inside quotes or not. If you are, spaces shouldn't count as seperators. Continue until the end of the input string.
Note that continuously adding characters to a string is not very effecient so maybe use wxStringBuffer for better performance. Or preallocate a large enough buffer with wxString::Alloc().