473,597 Members | 2,285 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replace unknown string via RegEx

Hi,

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

For example I have:

user_pref("netw ork.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 3641

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

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

For example I have:

user_pref("netw ork.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 ReplaceProxyVal ue(string Input, string ReplaceWith)
{
string pattern =
"(?<A>user_pref \\(\"network\\. proxy\\.http\", \")" +
"(?<Value>.+)(? <Z>\"\\))";

string input = "user_pref(\"ne twork.proxy.htt p\", \"server1\") ";
string replacePattern = "${A}" + ReplaceWith + "${Z}";

if (Regex.IsMatch( input, pattern)) {
return Regex.Replace(i nput, 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("netw ork.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.goo glegroups.com.. . Hi,

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

For example I have:

user_pref("netw ork.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("netw ork.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("netw ork.proxy.http_ port", 8080);
user_pref("netw ork.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("netw ork.proxy.http_ port", 8080);
user_pref("netw ork.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******** *************@g 10g2000cwb.goog legroups.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("netw ork.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("netw ork.proxy.http_ port", 8080);
user_pref("netw ork.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("netw ork.proxy.ftp", "server1");

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

so that I could do something like:

sNewConfig = replaceUnknownS tring(sOldConfi g,
"user_pref("net work.proxy.ftp" , "[unknown_value]"); ", "server2");

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

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

May 24 '06 #6
sorry,

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

sNewConfig = replaceUnknownS tring(sOldConfi g,
"user_pref("net work.proxy.ftp" , "[unknown_value]"); ", "server2");

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

so that sNewConfig would be like sOldConfig except that the relevant
line would now be:
user_pref("netw ork.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.goo glegroups.com.. .
sorry,

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

sNewConfig = replaceUnknownS tring(sOldConfi g,
"user_pref("net work.proxy.ftp" , "[unknown_value]"); ", "server2");

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

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

May 24 '06 #8

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:%2******** ********@TK2MSF TNGP02.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(""n etwork.proxy.ft p"",
""server1"" ); ";
Console.WriteLi ne(replaceUnkno wnString(sOldCo nfig,
"Server2")) ;
}

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

May 24 '06 #10

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

Similar topics

2
3073
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 last char to position 15. EX: 123456 transforms to 12345 6
3
2421
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
11340
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 variable on which I need to perform log and then divide the result with 3.980. While parsing such expressions, I need to find all occurence of date values and replace "/" character with "~" character. I need to do this only for date portion of the
4
3836
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, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
3
8249
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;}") Thanks, Craig
4
1589
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 alternative to this? Example: Property orgname() As String Get orgname = m_orgname End Get Set(ByVal Value As String)
9
4241
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). Why does this function replace the found reg ex. with the actual string "\t" and not a tab? (in the example below out of frustration I actually hardcoded the "\t") Private Shared Function replaceAll(ByVal strIn As String, ByVal strFind As...
4
3141
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 want to optimize it, so i make a static member instance instead of using static func of Regex; static Regex filter= new Regex(".*0\\.000000.*\n", RegexOptions.Compiled); And use the static instance to replace the string....
5
2573
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 @ shouldn't be replaced.
0
7893
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3889
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3932
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1243
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.