0

I have a pretty easy (imo) Regular expression which doesn' translate to C#. RegExr says it works, however testing it in C# doesn't work... Do I need to change anything to make it work in C#?

RegEx:

/https://subdomain[.1]domain[.1]com/(.*)(ashx|axd)/g

Replace:

http://subdomain.domain.com/$1$2

Input string:

<script src="https://subdomain.domain.com/ScriptResource.axd?d=SQ032obz8ZDKgHbn6XZ3X4xN0iWznAgAxgrdjOtFjVj-yUo-Lk-c2r4ciOs2uasdfjiemr4ciOs2uaGYfeYrYGiemnAQT9UzGoQVKs_JPL8l7TA1LjPYKexOcr4ciOs2uar4ciOs2uaHgpZf-tDxkb_le2NhBN0&amp;t=ffffffffb868b5f4" 
type="text/javascript"></script><script type="text/javascript">
//<![CDATA[
Sys.Application.initialize();
//]]>
</script>
5
  • bad!!! bad!! your target string contains / (in http://)!! escape them!! Commented Feb 23, 2012 at 15:18
  • You know, that you cannot use / in a regex without escaping them, or using different delimiters, right? Commented Feb 23, 2012 at 15:19
  • Can you post your C# code? The regex you've given wouldn't work either as the // following the protocol specifier would only parse as a single / for the regex I believe. There's plenty of documentation on the .Net Regex class and how to use it. Unfortunately Regex isn't a language in itself and has different implementations on pretty much every platform. Commented Feb 23, 2012 at 15:20
  • To be honest, you seem to replace https with http. Why don't you just do that? Why the other matching? Or do you want to extract the url? What is the desired output? Commented Feb 23, 2012 at 15:28
  • @Shai, @knittl, @Lazarus: the // isn't the problem and still works. It was the / and /g at beginning and end that caused the problem. Commented Feb 23, 2012 at 15:57

1 Answer 1

2

Your regex shouldnt contain / and /g. And instead [.1] write .

As I suspect you only want to change https into http you can just do that:

s = s.Replace("https:", "http:");

In case you want to replace the domain you can do that with regex:

  s = Regex.Replace(s, "https://[^/]+", "http://yourreplaced.domain.com", RegexOptions.Multiline);
Sign up to request clarification or add additional context in comments.

4 Comments

This works indeed! The [.1] is still valid and didn't have to be replaced. However don't I need the /g to make it replace all occurrences?
I don't need the /g however I do need the [.1]!
Show your desired output and I can help you further.
Added a regex for you. Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.