How can I make multiple regex check with string values?
I am using VS 2003 and Regex.
I wanted to spilt the text from richtextbox if it matches newlines or semicolon or "/" or"\n\x20*\n"etc few sequences.
If any of these matches, I need a split
For single sequences, I use this
Regex* regxterm =__gcnew Regex(S"\n\x20*\n");// very good \20=space, \u0020=unicode space
String* array[] = regxterm->Split(this->richTextBox1->Text);
for (int i=0;i<array->Length;i++)
{MessageBox::Show(this,array
,S"Test");}
How can I make multiple regex check with string values?
Thanks,
[973 byte] By [
Jil] at [2008-2-26]
gqlu,
For example, if I want to split the String everyline, I can use regex like "\n" to split.
If I need to split the String whenever it finds any one of "\n" or "\t" or ";" or "/".
If the string is like this: (Please ignore the braces {})
"
select * from dual;
select * from dual
/
select * from dual{tab};
select * from dual{tab}{tab}{space}{space};
select * from dual{\n}{\n}
"
This has to split the string into five parts with "select * from dual".
Thanks,
Jil at 2007-9-3 >

I got the solution from
http://www.dotnetcoders.com/web/Learning/Regex/regexobject.aspx
Here is the code:
private: void Split_with_MultiplePattern1()
{
String* input = this->richTextBox1->Text;
Regex* regex = new Regex(S"\n\\s*\n|;\\s*\n|;|\n\\s*\\/");
String* tokens[] = regex->Split(input);
for (int i = 0; i < tokens->Length; i++)
{
MessageBox::Show(this,tokens
,S"Test");
}
}
Jil at 2007-9-3 >

Yeah, I see. Actually, you can combine the multiple regex into one regex, something like this:
| | Regex rx = new Regex(";|/"); |
.
It means that either ';' or '/' character. But it's more complex for the whilespaces, maybe you can use the string.Trim() to pre-process. So in the two pre-condtions: 1. the real separator is ';' or '/'; 2. the whilespaces, i.e. tab, space, line-feed can't be used to separate the two statements, but they can be used before or after ';' & '/'. Then the following lines of code does what you want:
| | string test_string = "select * from dual1;select * from dual2/select * from dual3 ;select * from dual4 ;\nselect * from dual5";Regex rx = new Regex(";|/"); string[] array = rx.Split(test_string); for (int index = 0; index < array.Length; index++) { array[index] = array[index].Trim(); MessageBox.Show(array[index]); }
|
However, you can use the '\n', '\t' and ' ' to seperate the statements, but then, the regular expression will be rather complex. To do this, you may have to specify all the details about the whitespaces.
Hope this helps.
(Moderator: Code reformatted to aviod smileys [ i ])