RegEx Help needed

  • Thread starter Thread starter barry
  • Start date Start date
B

barry

Hi

str = "<IMG src="/MySite/images/placeholder.gif" width=5 border=0>";
str += "<img height=56 src="/MySite/images/logo/CodeBackground.gif"
width=538 border=0>";
str += "<IMG height=9 src="/MySite/images/Menu/Expand_minus_small.gif"
width=9 border=0>";

i need to change the above string value (str) to the following

<IMG src="/MyLocalFolder/placeholder.gif" width=5 border=0>
<img height=56 src="/MyLocalFolder/CodeBackground.gif" width=538 border=0>
<IMG height=9 src="/MyLocalFolder/Expand_minus_small.gif" width=9 border=0>

in short i want to change

/MySite/images/placeholder.gif ==> /MyLocalFolder/placeholder.gif
/MySite/images/logo/CodeBackground.gif ==> /MyLocalFolder/CodeBackground.gif
/MySite/images/Menu/Expand_minus_small.gif ==>
/MyLocalFolder/Expand_minus_small.gif

something similar to what Firefox or IE does when you SaveAs a web page

can some expert in regexpression help me out

barry
 
(?i)(?<=img.*?src=").*?(?=[\w\d]+\.[\w\d]+?")

This will capture the path of the image file only. Brief explanation:

(?i) - This regular expression is case-insensitive.
(?<=img.*?src=") Positive LookBehind. Match MUST be preceded by the
character sequence "img" followed by any sequence of characters (lazy match,
ends at beginning of next part of match), followed by the character sequence
"src=""
..*? - Match consists of zero or more characters (lazy match, ends at
beginning of next part of match)
(?=[\w\d]+\.[\w\d]+?") - Positive LookAhead. Match MUST be followed by a
character sequence consisting of one or more word or digit characters,
followed by a period, followed by (lazy match, ends at beginning of next
part of match) one or more word or digit characters, followed by a quote.

The positive LookAhead and LookBehind ensure that the preceding and
following parts of the match are not captured.

You can then replace that with your path.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
Thanks for your quick reply, can you please give me the full line for
Regex.Replace, i am going round the bend adding character to over come quote
in the string, like this

string str = "<IMG src=\"/MySite/images/placeholder.gif\" width=5
border=0>";
str += "<img height=56 src=\"/MySite/images/logo/CodeBackground.gif\"
width=538 border=0>";

str += "<IMG height=9 src=\"/MySite/images/Menu/Expand_minus_small.gif\"
width=9 border=0>";


string s = Regex.Replace(str,
"(?i)(?<=img.*?src=").*?(?=[\w\d]+\.[\w\d]+?")") , "Hello World/);

I have put "Hello World/" as the replacement.



(?i)(?<=img.*?src=").*?(?=[\w\d]+\.[\w\d]+?")

This will capture the path of the image file only. Brief explanation:

(?i) - This regular expression is case-insensitive.
(?<=img.*?src=") Positive LookBehind. Match MUST be preceded by the
character sequence "img" followed by any sequence of characters (lazy
match, ends at beginning of next part of match), followed by the character
sequence "src=""
.*? - Match consists of zero or more characters (lazy match, ends at
beginning of next part of match)
(?=[\w\d]+\.[\w\d]+?") - Positive LookAhead. Match MUST be followed by a
character sequence consisting of one or more word or digit characters,
followed by a period, followed by (lazy match, ends at beginning of next
part of match) one or more word or digit characters, followed by a quote.

The positive LookAhead and LookBehind ensure that the preceding and
following parts of the match are not captured.

You can then replace that with your path.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

barry said:
Hi

str = "<IMG src="/MySite/images/placeholder.gif" width=5 border=0>";
str += "<img height=56 src="/MySite/images/logo/CodeBackground.gif"
width=538 border=0>";
str += "<IMG height=9 src="/MySite/images/Menu/Expand_minus_small.gif"
width=9 border=0>";

i need to change the above string value (str) to the following

<IMG src="/MyLocalFolder/placeholder.gif" width=5 border=0>
<img height=56 src="/MyLocalFolder/CodeBackground.gif" width=538
border=0>
<IMG height=9 src="/MyLocalFolder/Expand_minus_small.gif" width=9
border=0>

in short i want to change

/MySite/images/placeholder.gif ==> /MyLocalFolder/placeholder.gif
/MySite/images/logo/CodeBackground.gif ==>
/MyLocalFolder/CodeBackground.gif
/MySite/images/Menu/Expand_minus_small.gif ==>
/MyLocalFolder/Expand_minus_small.gif

something similar to what Firefox or IE does when you SaveAs a web page

can some expert in regexpression help me out

barry
 
public static string replaceUrl(string str)
{
string replacement = "Hello World/";
// Properly escaped:
string pattern = "(?i)(?<=img.*?src=\").*?(?=[\\w\\d]+\\.[\\w\\d]+?\")";
Regex regX = new Regex(pattern);
return regX.Replace(str, replacement);
}

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP


barry said:
Thanks for your quick reply, can you please give me the full line for
Regex.Replace, i am going round the bend adding character to over come
quote in the string, like this

string str = "<IMG src=\"/MySite/images/placeholder.gif\" width=5
border=0>";
str += "<img height=56 src=\"/MySite/images/logo/CodeBackground.gif\"
width=538 border=0>";

str += "<IMG height=9 src=\"/MySite/images/Menu/Expand_minus_small.gif\"
width=9 border=0>";


string s = Regex.Replace(str,
"(?i)(?<=img.*?src=").*?(?=[\w\d]+\.[\w\d]+?")") , "Hello World/);

I have put "Hello World/" as the replacement.



(?i)(?<=img.*?src=").*?(?=[\w\d]+\.[\w\d]+?")

This will capture the path of the image file only. Brief explanation:

(?i) - This regular expression is case-insensitive.
(?<=img.*?src=") Positive LookBehind. Match MUST be preceded by the
character sequence "img" followed by any sequence of characters (lazy
match, ends at beginning of next part of match), followed by the
character sequence "src=""
.*? - Match consists of zero or more characters (lazy match, ends at
beginning of next part of match)
(?=[\w\d]+\.[\w\d]+?") - Positive LookAhead. Match MUST be followed by a
character sequence consisting of one or more word or digit characters,
followed by a period, followed by (lazy match, ends at beginning of next
part of match) one or more word or digit characters, followed by a quote.

The positive LookAhead and LookBehind ensure that the preceding and
following parts of the match are not captured.

You can then replace that with your path.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

barry said:
Hi

str = "<IMG src="/MySite/images/placeholder.gif" width=5 border=0>";
str += "<img height=56 src="/MySite/images/logo/CodeBackground.gif"
width=538 border=0>";
str += "<IMG height=9 src="/MySite/images/Menu/Expand_minus_small.gif"
width=9 border=0>";

i need to change the above string value (str) to the following

<IMG src="/MyLocalFolder/placeholder.gif" width=5 border=0>
<img height=56 src="/MyLocalFolder/CodeBackground.gif" width=538
border=0>
<IMG height=9 src="/MyLocalFolder/Expand_minus_small.gif" width=9
border=0>

in short i want to change

/MySite/images/placeholder.gif ==> /MyLocalFolder/placeholder.gif
/MySite/images/logo/CodeBackground.gif ==>
/MyLocalFolder/CodeBackground.gif
/MySite/images/Menu/Expand_minus_small.gif ==>
/MyLocalFolder/Expand_minus_small.gif

something similar to what Firefox or IE does when you SaveAs a web page

can some expert in regexpression help me out

barry
 
Hi

Thanks for your reply, i did manage to get it right before you could post
this message. But there is another issue for which i am posting another
message.

Barry


Kevin Spencer said:
public static string replaceUrl(string str)
{
string replacement = "Hello World/";
// Properly escaped:
string pattern =
"(?i)(?<=img.*?src=\").*?(?=[\\w\\d]+\\.[\\w\\d]+?\")";
Regex regX = new Regex(pattern);
return regX.Replace(str, replacement);
}

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP


barry said:
Thanks for your quick reply, can you please give me the full line for
Regex.Replace, i am going round the bend adding character to over come
quote in the string, like this

string str = "<IMG src=\"/MySite/images/placeholder.gif\" width=5
border=0>";
str += "<img height=56 src=\"/MySite/images/logo/CodeBackground.gif\"
width=538 border=0>";

str += "<IMG height=9 src=\"/MySite/images/Menu/Expand_minus_small.gif\"
width=9 border=0>";


string s = Regex.Replace(str,
"(?i)(?<=img.*?src=").*?(?=[\w\d]+\.[\w\d]+?")") , "Hello World/);

I have put "Hello World/" as the replacement.



(?i)(?<=img.*?src=").*?(?=[\w\d]+\.[\w\d]+?")

This will capture the path of the image file only. Brief explanation:

(?i) - This regular expression is case-insensitive.
(?<=img.*?src=") Positive LookBehind. Match MUST be preceded by the
character sequence "img" followed by any sequence of characters (lazy
match, ends at beginning of next part of match), followed by the
character sequence "src=""
.*? - Match consists of zero or more characters (lazy match, ends at
beginning of next part of match)
(?=[\w\d]+\.[\w\d]+?") - Positive LookAhead. Match MUST be followed by a
character sequence consisting of one or more word or digit characters,
followed by a period, followed by (lazy match, ends at beginning of next
part of match) one or more word or digit characters, followed by a
quote.

The positive LookAhead and LookBehind ensure that the preceding and
following parts of the match are not captured.

You can then replace that with your path.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

Hi

str = "<IMG src="/MySite/images/placeholder.gif" width=5 border=0>";
str += "<img height=56 src="/MySite/images/logo/CodeBackground.gif"
width=538 border=0>";
str += "<IMG height=9 src="/MySite/images/Menu/Expand_minus_small.gif"
width=9 border=0>";

i need to change the above string value (str) to the following

<IMG src="/MyLocalFolder/placeholder.gif" width=5 border=0>
<img height=56 src="/MyLocalFolder/CodeBackground.gif" width=538
border=0>
<IMG height=9 src="/MyLocalFolder/Expand_minus_small.gif" width=9
border=0>

in short i want to change

/MySite/images/placeholder.gif ==> /MyLocalFolder/placeholder.gif
/MySite/images/logo/CodeBackground.gif ==>
/MyLocalFolder/CodeBackground.gif
/MySite/images/Menu/Expand_minus_small.gif ==>
/MyLocalFolder/Expand_minus_small.gif

something similar to what Firefox or IE does when you SaveAs a web page

can some expert in regexpression help me out

barry
 
Back
Top