regular expressions

C

csharpula csharp

Hi,

Please help with the following regular expression:

Regex.Replace(contents,@"memsize = /"(/d+)/"","111111111111111111111")

while trying to build this expression :

memsize = "512"
"memsize = \"(d+)\""

I am getting an error: Invalid / charecter.

What's wrong? Thanks
 
F

Family Tree Mike

Hi,

Please help with the following regular expression:

Regex.Replace(contents,@"memsize = /"(/d+)/"","111111111111111111111")

while trying to build this expression :

memsize = "512"
"memsize = \"(d+)\""

I am getting an error: Invalid / charecter.

What's wrong? Thanks

I think the expression you want is: memsize\s+=\s+\"(\d+)\"

This makes your line:
Regex.Replace(contents,@"memsize\s+=\s+\"(\d+)\"",
"111111111111111111111")

Oh, and you need ; at the end of each code line!
 
A

Arne Vajhøj

Please help with the following regular expression:

Regex.Replace(contents,@"memsize = /"(/d+)/"","111111111111111111111")

while trying to build this expression :

memsize = "512"
"memsize = \"(d+)\""

I am getting an error: Invalid / charecter.

What's wrong? Thanks

It does not compile.

I guess that you want one of the 4 variations showed below:

using System;
using System.Text.RegularExpressions;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
string contents = @"memsize = ""12345""";
Console.WriteLine(Regex.Replace(contents, @"memsize =
""(\d+)""","111111111111111111111"));
Console.WriteLine(Regex.Replace(contents, "memsize =
\"(\\d+)\"","111111111111111111111"));
Console.WriteLine(Regex.Replace(contents, @"(memsize =
)""(\d+)""","${1}111111111111111111111"));
Console.WriteLine(Regex.Replace(contents, "(memsize =
)\"(\\d+)\"","${1}111111111111111111111"));
Console.ReadKey();
}
}
}

Pick the one that meets your requirements.

Arne
 
A

Arne Vajhøj

I think the expression you want is: memsize\s+=\s+\"(\d+)\"

This makes your line:
Regex.Replace(contents,@"memsize\s+=\s+\"(\d+)\"",
"111111111111111111111")

That does not compile either - difference between
@ and \ techniques.

Arne
 
T

treuss

That does not compile either - difference between
@ and \ techniques.

Arne

This one should work:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RegexReplaceTest {
class Program {
static void Main(string[] args) {
string content = @"memsize = ""12345""";
string memory = "111111111111111111111";
string processedContent = Replace(content, memory);
Console.WriteLine(string.Format("{0} ==> {1}", content,
processedContent));
Console.ReadKey();
}

public static string Replace(string content, string
replacement) {
return Regex.Replace(content, @"(memsize\s=\s"")\d+("")",
@"${1}" + replacement + "${2}");
}
}
}
 
T

treuss

That does not compile either - difference between
@ and \ techniques.

Arne

This one should work:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RegexReplaceTest {
class Program {
static void Main(string[] args) {
string content = @"memsize = ""12345""";
string memory = "111111111111111111111";
string processedContent = Replace(content, memory);
Console.WriteLine(string.Format("{0} ==> {1}", content,
processedContent));
Console.ReadKey();
}

public static string Replace(string content, string
replacement) {
return Regex.Replace(content, @"(memsize\s=\s"")\d+("")",
@"${1}" + replacement + "${2}");
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top