If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
void RemoveTrailingLineNumbersFrame::OnSearchClick(wxCommandEvent& event)
{
wxString exp=text1->GetValue();// ^\d+\.
wxRegEx reg(exp);
if (reg.IsValid()) wxLogStatus(wxT("Valid regular expression."));
long cnt=text2->GetNumberOfLines();
for(int i=0;i<cnt;i++)
{
//multi line wxTextCtrl text2 with initial values as listed above 0001. this is line 0001.; etc
wxString content=text2->GetLineText(i);
if( reg.Matches( content))
{
reg.Replace(&content,wxEmptyString);
*text2<<content<<wxT("\r\n");
}
}
}
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
I won't help you with wxRegEx but if all you need is to remove those numbers at the beginning of the line(i.e. everything before and including the first space), just
seems to be simpler and perhaps more efficient solution. You can also use wxString::Mid(), assuming the string you want to "remove" has a fixed length.
Thanks.It should work perfectly.But in my case,the result is weird :both wxTextCtrl and wxMessageBox printed as below,that is,one more wrong line 'is line 0001.;'.
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
Thanks.It solved the issue how to remove the line number.
Still wanna know why the regex version failed as I am not so faimilar with wxRegEx.
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
I am sorry, I haven't used wxRegEx before. Are you sure your regular expression - "^\d+\" - is correct? wxRegEx's IsValid() returns false for it for me (wxWidgets 2.9.5 on MSW), perhaps it doesn't like the trailing slash. Anyway, I have tried this
wxRegEx regex(wxS("^(\\d+. )"));
wxString line(wxS("0001. This is line 0001.;"));
wxASSERT( regex.IsValid() );
if ( regex.Matches(line) /* regex.Replace(&line, wxEmptyString) == 1 */ )
{
wxMessageBox(regex.GetMatch(line));
// wxMessageBox(line);
}
else
{
wxMessageBox(_("Couldn't replace a part of the string"));
}
and I still don't get a match (the regular expression is not for just a number but for a space and dot after the number too). No idea what I'm doing wrong here.
^\d+\. (there is a dot here) should be correct as I have a workable test app in another language (freepascal+lazarus).
Yes,you are right,in wx the reg.isValid() returned true but reg.Match(text2->GetLineText(i)) returned false which I don't know where the issue is.
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
wxRegEx regex(wxS("^\\d+\\. "), wxRE_ADVANCED);
wxString line(wxS("0001. This is line 0001.;"));
wxASSERT( regex.IsValid() );
if ( regex.Replace(&line, wxEmptyString) == 1 )
{
wxMessageBox(line); // produces "This is line 0001.;"
}
else
{
wxMessageBox(_("Line string is not in valid format"));
}
In default (= extended) syntax mode one can use a range specifier, i.e. the expression literal would be (without extra slash) "^[0-9]+\. ".