• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

Regular Expression Replace String

I am looking for a way to replace the http in a string with https when I am doing a regex replace.  I want to leave the rest of the string alone except for the first 4 characters.  Thanks!
0
VBBRett
Asked:
VBBRett
  • 2
  • 2
2 Solutions
 
Terry WoodsIT GuruCommented:
Try this:

my_string = Regex.Replace(my_string, @"http(?!s)", "https");

Open in new window

0
 
Easwaran ParamasivamCommented:
When you are sure to replace all http with https you could use below one.

my_string = Regex.Replace(my_string, @"http", "https");
0
 
Terry WoodsIT GuruCommented:
@EaswaranP, that will also change https to httpss.
0
 
Easwaran ParamasivamCommented:
I do agree. Assumption is that there is no https in his text.
0
 
Srihari03Commented:
Try this. If your URL having https, it will leave as such. If your URL having http it will change to https.

str = Regex.Replace(str, @"\b"+"http"+@"\b", "https");
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now