473,396 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

replace unknown string via RegEx

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
11 3617

<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
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
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
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
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
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
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

"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
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
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
Thanks so much James,

THAT DID IT !!

I appreciate your help very much

May 25 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bill Mittenzwey | last post by:
Is there a way to do a Regex replace on a string to stuff some variable amount of spaces into a location of a string? I need an expression which would take an unknown length string and move the...
3
by: RickN | last post by:
What is the best way to open a file and perform a search and replace for multiple char values. Thanks, RickN
5
by: Mahesha | last post by:
Hello, I need help in replacing one string pattern with another. Ex: I have a financial security expression like log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002) Here "T 3.25 6/24/2004" is a...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
4
by: Derek Martin | last post by:
I have an object with several string elements that I would like to check for invalid characters in the properties of each element. Can I use string.replace to do that or is there a better...
9
by: Whitless | last post by:
Okay I am ready to pull what little hair I have left out. I pass the function below my String to search, my find string (a regular expression) and my replace string (another regular expression)....
4
by: Morgan Cheng | last post by:
In my case, I have to remove any line containing "0.000000" from input string. In below case, it takes about 100 ms for 2k size input string. Regex.Replace(inputString, ".*0\\.000000.*\n", ""); I...
5
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.