创建解析器来处理字符串 [英] Creating a Parser to handle a string

查看:61
本文介绍了创建解析器来处理字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要创建一个解析器,以便可以复制包含整数的字符串,并根据用户的需要更改整数。



示例:



Bob在2个篮子里有6个苹果

有2个篮子里有6个苹果棒





只是想知道如何解决这个问题,我已经想了几天了,需要另外一个想法来提出一个布局。



到目前为止我已经有了一个下拉菜单,它可以选择上述任一字符串,将它们放入一个富文本框中,用户可以调整所选字符串中的任何一个int值。 />


我认为解析器应该断开字符串,并且字符串的每个部分都应该保存在表的单独列中吗?

因此,当用户决定更改整数时,解析器可以检查整数应该在哪里(如果移动)?

请帮助... hpw你会接近吗?这个?谢谢你



我真的很困惑如何处理这种情况。



任何帮助都非常多赞赏。



亲切的问候Matt。



编辑。

不是解析器我遇到了麻烦,因为我可以在进入表之前创建一个过程,因此解析器会运行用户输入的字符串。它是如何布置形式的,任何人都做了类似的建议吗?

need to create a parser so that a string containing intergers can be replicated, with the intergers changed as the user sees fit.

Example:

"Bob has 6 apples in 2 baskets"
"there are 2 baskets which have 6 of bobs apples"


just wondering how to go about this, ive been thinking for a few days now, need an other mind to come up with a layout.

so far ive got a drop down menu which can select either of the above strings, places them into a rich text box and the user can adjust either of the int values in their selected string.

Am i right in thinking the parser should break the string down, and each ''part'' of the string should be held in seperate columns of a table?
So that when the user decides to changes the integers then the parser can check where, (if moved) the integer should be?
Please help... hpw would you approach this? Thanks you

Im really confused in how to handle such a situation.

Any help is very much appreciated.

Kind Regards Matt.

Edit.
Its not the parser im having trouble with, as i can create a procedure before entering to the table, so the parser runs over the user''s entered string. its how to lay out form, any one done something similar that can advise?

推荐答案

我仍然没有100%清楚你追求的是什么。



我理解你需要的是一个占位符吗?相当于一个只读变量,你可以用适当的占位符(变量)填充新的句子结构?



如果是这样,将命名正则表达式吗?



如果我是对的,那么您可以查看文章:http://www.codeproject.com/Articles/42304/Enhanced-String-Handling -III



干杯,

Avi
It is still not 100% clear to me what it is that you are after.

Would I be correct in my understanding that you need is a place holder? An equivalent of a readonly-variable that you would be able to "stuff" the new sentence structure with the appropriate place holder (variable)?

If so, will named regular expressions do?

If I am correct, then you may care to check out the article: http://www.codeproject.com/Articles/42304/Enhanced-String-Handling-III

Cheers,
Avi


实际上你需要一个非常简单的解析器。您可以简单地使用空格作为分隔符拆分字符串,然后在结果数组项上找到整数( int.TryParse 就足够了)并跟踪它们的索引(amybe使用另一个数组或列表)。这样,生成所需的输出将是微不足道的。
Well you need a very simple parser, actually. you might simply split the string using the space as separator and then, on the resulting array items, find the integers (a int.TryParsewould suffice) and keep track of their indices (amybe using another array or a list). This way, generating the required output would be trivial.


您需要的最佳ans。

如果您想要字符串中的数字,只需使用正则表达式。

请参阅以下代码块。

The best ans as you need.
If you want to numbers from string just simply use regex.
See the following code block.
Regex reges = new Regex(@"\d+");
Match match = regex.Match("test 66");

if (match.Success)
{
    Console.WriteLine(string.Format("RegEx found " + match.Value + " at position " + match.Index.ToString()));
}
else
{
    Console.WriteLine("You didn't enter a string containing a number!");
}



以及少量代码将对您有所帮助。


and also small amount of code will helps you.

result = Regex.Match(FeatchFromStr, @"\d+").Value;



如果你不想使用正则表达式,那就很难解决这个问题。


And in very old trick to solve this if you not want to use regex.

string source = "str123";
string destination = string.Empty;
int FinalResult;
for (int i=0; i< source.Length; i++)
{
    if (Char.IsDigit(source[i]))
        destination += source[i];
}
if (destination.Length>0)
    FinalResult = int.Parse(destination);



即使linq也可以帮助你。


Even linq can also help you.

string Source = "3241k2h341k2341khlkhjl";
var Numbers = (from s in Source
           where char.IsDigit(s)
           select s).ToArray();
Console.WriteLine(new string(Numbers));


这篇关于创建解析器来处理字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆