小编don*_*too的帖子

Regex.Replace中的MatchEvaluator如何工作?

这是输入字符串23x * y34x2.我希望" * "在每个数字之后插入(由空格包围的星形)后跟字母,并在每个字母后跟数字.所以我的输入字符串看起来像这样:23 * x * y * 34 * x * 2.

这是完成这项工作的正则表达式:@"\d(?=[a-z])|[a-z](?=\d)".这是我写的插入的函数" * ".

Regex reg = new Regex(@"\d(?=[a-z])|[a-z](?=\d)");   
MatchCollection matchC;
matchC = reg.Matches(input);
int ii = 1;
foreach (Match element in matchC)//foreach match I will find the index of that match
{
    input = input.Insert(element.Index + ii, " * ");//since I' am inserting " * " ( 3 characters )
    ii += 3;                                        //I must …
Run Code Online (Sandbox Code Playgroud)

.net c# regex replace matchevaluator

43
推荐指数
1
解决办法
3万
查看次数

关于编译器及其工作原理的问题

这是释放单链表的内存的C代码.它是使用Visual C++ 2008编译的,代码可以正常工作.

/* Program done, so free allocated memory */
current = head;
struct film * temp;
temp = current;
while (current != NULL)
{
    temp = current->next;
    free(current);
    current = temp;
}
Run Code Online (Sandbox Code Playgroud)

但是我也遇到过(甚至在书中)相同的代码:

/* Program done, so free allocated memory */
current = head;
while (current != NULL)
{
    free(current);
    current = current->next;
}
Run Code Online (Sandbox Code Playgroud)

如果我使用VC++ 2008编译该代码,程序崩溃是因为我首先释放当前的电流,然后分配current-> current.但显然如果我用其他编译器(例如,书籍作者使用的编译器)编译此代码,程序将起作用.所以问题是,为什么用特定编译器编译的代码工作?是因为编译器将指令放在记住current-> next的二进制文件中,尽管我释放了当前的而我的VC++却没有.我只想了解编译器的工作原理.

c c++ compiler-construction

12
推荐指数
2
解决办法
750
查看次数

如何通过正则表达式使用变量?

这是输入字符串:23x^45*y or 2x^2 or y^4*x^3

^[0-9]+在 letter 之后进行匹配x。换句话说,我匹配x后跟^数字。问题是我不知道我正在匹配x,它可能是我在 char 数组中作为变量存储的任何字母。

例如:

foreach (char cEle in myarray) // cEle is letter in char array x, y, z, ...
{
    match CEle in regex(input) //PSEUDOCODE
}
Run Code Online (Sandbox Code Playgroud)

我是正则表达式的新手,我知道如果我定义正则表达式变量就可以做到这一点,但我不知道如何做。

.net c# regex

3
推荐指数
1
解决办法
9889
查看次数

我可以一步编写这个正则表达式吗?

这是输入字符串"23x + y-34 x + y + 21x - 3y2-3x-y + 2".我希望用空格包围每个'+'和' - '字符,但前提是它们不是从左侧或右侧都已完全消失.所以我的输入字符串看起来像"23x + y - 34 x + y + 21x - 3y2 - 3x - y + 2".我写了这个代码来完成这项工作:

    Regex reg1 = new Regex(@"\+(?! )|\-(?! )");
    input = reg1.Replace(input, delegate(Match m) { return m.Value + " "; });
    Regex reg2 = new Regex(@"(?<! )\+|(?<! )\-");
    input = reg2.Replace(input, delegate(Match m) { return " " + m.Value; });
Run Code Online (Sandbox Code Playgroud)

解释:reg1 //匹配'+'后跟任何不是''(空格)的字符或' - '相同的字符

reg2 //同样的事情,我匹配'+'或' - '不在前面''(空白)

委托1和2只在m.Value之前和之后插入""(匹配值)

问题是,有没有办法只创建一个正则表达式和一个代表?即一步完成这项工作?我是正则表达式的新手,我想学习有效的方法.

.net c# regex replace

0
推荐指数
1
解决办法
94
查看次数

标签 统计

.net ×3

c# ×3

regex ×3

replace ×2

c ×1

c++ ×1

compiler-construction ×1

matchevaluator ×1