By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,428 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

replace unknown string via RegEx

P: n/a
Hi,

i'm currently trying to replace an unknown string using regular
expressions.

For example I have:

user_pref("network.proxy.http", "server1")

What do I have to do to replace the "server1" part (which could be
anything else e.g. "test2") with a custom string.

May 23 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a

<ja*******@gmx.de> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hi,

i'm currently trying to replace an unknown string using regular
expressions.

For example I have:

user_pref("network.proxy.http", "server1")

What do I have to do to replace the "server1" part (which could be
anything else e.g. "test2") with a custom string.


private string ReplaceProxyValue(string Input, string ReplaceWith)
{
string pattern =
"(?<A>user_pref\\(\"network\\.proxy\\.http\", \")" +
"(?<Value>.+)(?<Z>\"\\))";

string input = "user_pref(\"network.proxy.http\", \"server1\")";
string replacePattern = "${A}" + ReplaceWith + "${Z}";

if (Regex.IsMatch(input, pattern)) {
return Regex.Replace(input, pattern, replacePattern);
} else {
return Input;
}
}

HTH :)

Mythran

May 23 '06 #2

P: n/a
First, you need to define your business rules. Without making any reference
to an example, what is the format of the string you're working with? What
are the rules that define the pattern of characters you're looking to
replace?

For example, you gave the following example:
user_pref("network.proxy.http", "server1")
Obviously, the "server1" part isn't the only part of the string that's going
to be different (or is it?). If so, what is it that you are looking for in
what sort of a pattern in a string?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

<ja*******@gmx.de> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com... Hi,

i'm currently trying to replace an unknown string using regular
expressions.

For example I have:

user_pref("network.proxy.http", "server1")

What do I have to do to replace the "server1" part (which could be
anything else e.g. "test2") with a custom string.

May 23 '06 #3

P: n/a
Thanks for your help.
Mybe I need to clear things up a bit:
I want to change specific settings within my Firefox config (prefs.js).

The folowing line for example tells Firefox to use "server1" as the
proxy server for http-traffic:

user_pref("network.proxy.http", "server1");

Since my program doesn't know the server name, I can't do a simple
String.Replace.
I want to load the content of the config file into a string and have my
method replace whatever string it find's to be the server name with
another string (e.g. "server2").
This has also to be done for such settings as:

user_pref("network.proxy.http_port", 8080);
user_pref("network.proxy.ftp", "huds13");
etc.

Mythran's approach works so far, so I think I will use it for my
purposes ;-)

May 24 '06 #4

P: n/a
Okay, now you've given 2 examples instead of one. And I note that you have a
working solution. But for future reference, let me explain how such a
problem needs to be analyzed:
This has also to be done for such settings as:

user_pref("network.proxy.http_port", 8080);
user_pref("network.proxy.ftp", "huds13");
etc.
Again, this is 2 examples, each of which seems to have some things in
common. But I don't know that, and neither do you, until you define what the
format rules are that apply across the board.

For example, one might derive the following rules from your 2 examples:

1. The first part of the string is always the token string "user_pref".
2. This is always followed by a set of parentheses, with data in between,
followed by a semicolon.
3. Inide the set of parentheses is a "name, value" pair.
4. The "name" part is a string literal enclosed in double quotes, followed
by a comma and optional space(s).
5. The "value" part is a token which can be of any data type. If it is a
string, it is enclosed in double quotes. If it is numeric, it is not
enclosed in double quotes.
6. Each configuration value is on a single line.

Of the above 5 rules, how many are true? I cannot tell for sure from your
example. For instance, *is* the first part of the string *always*
"user_pref"? Is this *always* followed by a set of parentheses and a
semicolon? Are there *always* 2 values inside the parentheses? Is the first
value *always* a string with double quotes surrounding? Is the space
optional after the comma? Can there be more than one space after the comma?
How does one "punctuate" (for example) a date value as the second value? Can
a configuration statement span multiple lines?

A computer is unable to guess. It must know the rules to parse the file. And
if you want to write instructions for parsng a string or strings, *you must
derive the rules.* Otherwise, exceptions occur. Once you define an exact and
accurate set of rules, the Regular Expression practically writes itself.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

<ja*******@gmx.de> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com... Thanks for your help.
Mybe I need to clear things up a bit:
I want to change specific settings within my Firefox config (prefs.js).

The folowing line for example tells Firefox to use "server1" as the
proxy server for http-traffic:

user_pref("network.proxy.http", "server1");

Since my program doesn't know the server name, I can't do a simple
String.Replace.
I want to load the content of the config file into a string and have my
method replace whatever string it find's to be the server name with
another string (e.g. "server2").
This has also to be done for such settings as:

user_pref("network.proxy.http_port", 8080);
user_pref("network.proxy.ftp", "huds13");
etc.

Mythran's approach works so far, so I think I will use it for my
purposes ;-)

May 24 '06 #5

P: n/a
I just wanted to find a way to create a function which I could pass two
parameters: the line in the config file:
user_pref("network.proxy.ftp", "server1");

and the string to replace the placeholder "[unknown]" with (e.g.
"server2")

so that I could do something like:

sNewConfig = replaceUnknownString(sOldConfig,
"user_pref("network.proxy.ftp", "[unknown_value]"); ", "server2");

where sOldConfig would be a string containing several lines of a file
(one of those lines is: user_pref("network.proxy.ftp", "server1");

so that sNewConfig would be like sOldConfig except that the relevant
line would now be:
user_pref("network.proxy.ftp", "server2");

May 24 '06 #6

P: n/a
sorry,

it should read like this:
I want to do something like this:

sNewConfig = replaceUnknownString(sOldConfig,
"user_pref("network.proxy.ftp", "[unknown_value]"); ", "server2");

where sOldConfig would be a string containing several lines of a file
(one of those lines is: user_pref("network.proxy.ftp", "server1");

so that sNewConfig would be like sOldConfig except that the relevant
line would now be:
user_pref("network.proxy.ftp", "server2");

May 24 '06 #7

P: n/a
I'm afraid not. A computer would never understand "something like this." A
computer understands exact rules and specific instructions. Something like
this:

1. The first part of the string is always the token string "user_pref".
2. This is always followed by a set of parentheses, with data in between,
followed by a semicolon.
3. Inide the set of parentheses is a "name, value" pair.
4. The "name" part is a string literal enclosed in double quotes, followed
by a comma and optional space(s).
5. The "value" part is a token which can be of any data type. If it is a
string, it is enclosed in double quotes. If it is numeric, it is not
enclosed in double quotes.
6. Each configuration value is on a single line.

*That* can be translated into a regular expression.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

If the truth hurts, wear it.

<ja*******@gmx.de> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
sorry,

it should read like this:
I want to do something like this:

sNewConfig = replaceUnknownString(sOldConfig,
"user_pref("network.proxy.ftp", "[unknown_value]"); ", "server2");

where sOldConfig would be a string containing several lines of a file
(one of those lines is: user_pref("network.proxy.ftp", "server1");

so that sNewConfig would be like sOldConfig except that the relevant
line would now be:
user_pref("network.proxy.ftp", "server2");

May 24 '06 #8

P: n/a

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
I'm afraid not. A computer would never understand "something like this." A
computer understands exact rules and specific instructions. Something like
this:

1. The first part of the string is always the token string "user_pref".
2. This is always followed by a set of parentheses, with data in between,
followed by a semicolon.
3. Inide the set of parentheses is a "name, value" pair.
4. The "name" part is a string literal enclosed in double quotes, followed
by a comma and optional space(s).
5. The "value" part is a token which can be of any data type. If it is a
string, it is enclosed in double quotes. If it is numeric, it is not
enclosed in double quotes.
6. Each configuration value is on a single line.

*That* can be translated into a regular expression.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull


As much as my code works doesn't matter. What matters is numbskull is
correct. My code would have been written better to suite your needs had you
been more specific. But since you solved it anyways, it's fine for now :)

:P Kevin, love yer sig..

Mythran

May 24 '06 #9

P: n/a
static void Main(string[] args)
{
string sOldConfig = @"user_pref(""network.proxy.ftp"",
""server1""); ";
Console.WriteLine(replaceUnknownString(sOldConfig,
"Server2"));
}

static string replaceUnknownString(string sOldConfig, string
sNewValue)
{
Regex regex = new
Regex(@"user_pref\(""([\w\.]+)"",\s*([^\)]+)\);");
string replacepattern = string.Format(@"user_pref(""$1"",
""{0}""); ", sNewValue);
return regex.Replace(sOldConfig,replacepattern);
}

May 24 '06 #10

P: n/a
Ooops, I meant to include the Interpretation of that regex pattern (via
Regex Workbench)

user_pref("
Capture
Any character in "\w\."
+ (one or more times)
End Capture
",
Any whitespace character
* (zero or more times)
Capture
Any character not in ")"
+ (one or more times)
End Capture
);

May 24 '06 #11

P: n/a
Thanks so much James,

THAT DID IT !!

I appreciate your help very much

May 25 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.