|
Sorry for my poor English.
My Question is: How to get which group was matched after a regex.match with grouped pattern string?
for instance:
string strPattern = "\Wo*e\W|\Wt*o\W|three|four";
string strTextToSearch = "there are two apples on the table";
Regex r = new Regex(strPattern);
Match m = r.Match(strTextToSearch);
if (m.Success)
{
//how can i know it is keyword "\Wt*o\W" was matched but not the others?
}
|
|
|
|
|
newproger wrote:
Nightmare with Regular Expression
YES you're right(not left) RegExp==NIGHTMARE
I'm sorry I just feel like joking. I can't help it.
|
|
|
|
|
Groups are automatically named according to their occurance order, but you can also name them yourself (this is called "named groups"). For example, the following regular expression has three named groups:
The (?<ANIMAL>cat|dog|penguin|langolier) (?<TRANSPORT>walked|waddled|flew) down the (?<PATH>street|yellow brick road|slope)\.
|
|
|
|
|
Thanks for reply.
But in my program, the pattern string is generated at runtime(keywords inputed by user), I can't know how many keywords in design time, so I can't set fixed group name in codes.
|
|
|
|
|
newproger wrote: Regex r = new Regex(strPattern);
Match m = r.Match(strTextToSearch);
if (m.Success)
{
//how can i know it is keyword "\Wt*o\W" was matched but not the others?
}
maube something like this:
m.Groups[1].Value
m.Groups[1].Index
|
|
|
|
|
i notice that in c# or c++, we normally indicate the size of an array.
string[] strArray = new string[5];
strArray[0] = "Ronnie";
strArray[1] = "Jack";
strArray[2] = "Lori";
strArray[3] = "Max";
strArray[4] = "Tricky";
Another way is like this:
string[] strArray = new string[] {"Ronnie", "Jack", "Lori", "Max", "Tricky"};
Both have fixed size. is there a way to create an array which can store unlimited amount of data?
I know of ArrayList but it is not available in compact framework.
|
|
|
|
|
What about List?
It has all the functionality of array/arrayList.
|
|
|
|
|
okay my bad. arraylist is available in compact framework.
however, im gonna try your suggestion instead. List looks like a better choice =)
|
|
|
|
|
Arrays in the .NET CLR are not resizable. Once they are created, that's it. In order to change their size, you'd have to create a new array of the required size, copy the data over, then destroy the original array.
|
|
|
|
|
|
There is something called Redim which used to resize the array at later stage, or the best you can do is go for arraylist
|
|
|
|
|
C# doesn't have a ReDim method like VB.NET does.
And, in VB.NET ReDim does NOT resize an array. Behind the scenes it has to do the exact same thing you'd do manually. It creates a new array of the required size, copies the data to it, then destroys the original array.
|
|
|
|
|
Actually, all .NET languages have access to the static "System.Array.Resize" method (since .NET Framework 2, I believe) - but as you said, there's no magic - it just does the same copying that VB's ReDim does. David Anton
Convert between VB, C#, C++, & Java
www.tangiblesoftwaresolutions.com
|
|
|
|
|
Yeah, I was saying that C# didn't have an equiv keyword to VB.NET's ReDim.
|
|
|
|
|
Just got back into writing code after 10 years. This is a infix to postfix converter. Can someone please critique this...
thanks
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace Infix2Postfix
{
public class Program
{
static void Main(string[] args)
{
#region variable declarations
String stringInfix = "a+b/c-d";
StringBuilder SbPostfix = new StringBuilder();
Stack<char> stackGeneral = new Stack<char>();
Stack<char> stackOperator = new Stack<char>();
char[] tempArray = stringInfix.ToCharArray();
Array.Reverse(tempArray);
Regex regexAlphaNum = new Regex("[a-zA-Z0-9]");
char temp;
#endregion
#region logic
foreach (char ch in tempArray)
stackGeneral.Push(ch);
foreach (char ch in stackGeneral)
{
if (regexAlphaNum.IsMatch(Convert.ToString(ch)))
SbPostfix.Append(ch);
else if (stackOperator.Count == 0)
stackOperator.Push(ch);
else if (stackOperator.Count != 0)
switch (ch)
{
case '+':
temp = stackOperator.Peek();
if (temp == '*' || temp == '/')
{
while (stackOperator.Count > 0)
SbPostfix.Append(stackOperator.Pop());
stackOperator.Push(ch);
}
else
stackOperator.Push(ch);
break;
case '-':
temp = stackOperator.Peek();
if (temp == '*' || temp == '/')
{
while (stackOperator.Count > 0)
SbPostfix.Append(stackOperator.Pop());
stackOperator.Push(ch);
}
else
stackOperator.Push(ch);
break;
case '*':
temp = stackOperator.Peek();
if (temp == '*' || temp == '/')
{
while (stackOperator.Count > 0)
SbPostfix.Append(stackOperator.Pop());
stackOperator.Push(ch);
}
else
stackOperator.Push(ch);
break;
case '/':
temp = stackOperator.Peek();
if (temp == '*' || temp == '/')
{
while (stackOperator.Count > 0)
SbPostfix.Append(stackOperator.Pop());
stackOperator.Push(ch);
}
else
stackOperator.Push(ch);
break;
}
}
while (stackOperator.Count > 0)
SbPostfix.Append(stackOperator.Pop());
#endregion
#region output to screen
Console.WriteLine("Equation in infix notation: {0}", stringInfix);
Console.Write("Equation in postfix/RPN notation: {0}", SbPostfix);
Console.ReadKey();
#endregion
}
}
}
|
|
|
|
|
I don't think you need tempArray.
How does it handle malformed input?
Here's[^] my infix-to-postfix code; there are others on here.
|
|
|
|
|
It only handles inputs in a*b-c/d format. The code is very basic, written in an non elegant way. I will look at your code, it might give me some insights.
|
|
|
|
|
Here I am criticizing it
It's too long and too late at night for me . Now it's joke time :cool
|
|
|
|
|
|
and i.push()
|
|
|
|
|
|
I would fire you on the spot for putting regions in a method and not maintaining consistent capitalization of variables.
|
|
|
|
|
lol...I know exactly which variable name you are referring to.
|
|
|
|
|
Why are regions in a method a bad idea? I ask this risking sounding like a fool.
|
|
|
|
|
Regions are intended to be used in GROUPING methods not splitting one method's body.
[EDIT]
Example:
#region Events//put here all/most of the event(s) handling code
#region Drawing//put here all the drawing methods
#region YouNameIt// put here all the...
[/EDIT]
|
|
|
|