[GO] [Ruby] How to replace only a part of the string matched by the regular expression?

problem

Is it possible to change only http of URL ending with .png to https when there is a character string like the following?

str = 'kkk<a href="http://abcdege/hoge222/bar/t22est.md">aaab.png 
bbb<a href="http://abcdege/ho22aage/bddfear/ted2st.png ">aaak
ubotabbbccxcb <a href="http://abcdege/22hoge/b22ar/tfeest.md">aa23a
bbkubotasbbb <a href="http://abcdege/hoffee11ge/bar/test.html">appkub
otapoooabbb<a href="http://abcdege/ho22aage/bddfear/ted2swwt.png ">ab
ddbbb.png'

Consideration

I was quite worried. I tried to replace it using ruby's gsub (regular expression, string) method.

So, I wondered if there is a way to replace only a part of the match (URL) (http part this time).

The result of thinking

I couldn't think of it with the above idea. In the first place, the idea of using only the gsub (regular expression, string) method may not have been good. There may be a way to use something like a regular expression block. ** However, in the end, I was able to create a procedure that would lead to the same result. ** **

Replace with 2-step sub.

Outline of the algorithm considered

".URL starting with png".gsub(http, "https")

It is a flow to replace with.

Details of the algorithm considered (finished product)

First, extract the URL from the string. I wanted to use String # scan here, but it seems that there is already a method called" Extract URL from string ". .. (too amazing) Reference: Extract URL from string --Ruby Tips!

URI.extract(str)
#=> ["http://abcdege/hoge222/bar/t22est.md", "http://abcdege/ho22aage/bddfear/ted2st.png ", "http://abcdege/22hoge/b22ar/tfeest.md", "http://abcdege/hoffee11ge/bar/test.html", "http://abcdege/ho22aage/bddfear/ted2swwt.png "]

Then use these to determine and replace each one.

(Final product)
URI.extract(str).each do |uri|
  if /.*(\.png)$/.match(uri)
    new_uri = uri.sub("http","https")
    str = str.sub(uri, new_uri)
  end
end
str
#=> "kkk<a href=\"http://abcdege/hoge222/bar/t22est.md\">aaab.png \nbbb<a href=\"https://abcdege/ho22aage/bddfear/ted2st.png\">aaak\nubotabbbccxcb <a href=\"http://abcdege/22hoge/b22ar/tfeest.md\">aa23a\nbbkubotasbbb <a href=\"http://abcdege/hoffee11ge/bar/test.html\">appkub\notapoooabbb<a href=\"https://abcdege/ho22aage/bddfear/ted2swwt.png\">ab\nddbbb.png "

I feel that the point is to replace it with a two-step sub.

Recommended Posts

[Ruby] How to replace only a part of the string matched by the regular expression?
Change the string to be replaced according to the matched string by replacing with Python regular expression
How to connect the contents of a list into a string
How to save only a part of a long video using OpenCV
How to calculate the volatility of a brand
Cut a part of the string using a Python slice
[Ubuntu] How to delete the entire contents of a directory
How to test the attributes added by add_request_method of pyramid
How to shuffle a part of a Python list (at random.shuffle)
How to find the scaling factor of a biorthogonal wavelet
Get the matched string with a regular expression and reuse it when replacing on Python3
How to quickly count the frequency of appearance of characters from a character string in Python?
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
How to determine the existence of a selenium element in Python
I tried to make a regular expression of "amount" using Python
How to check the memory size of a variable in Python
I tried to make a regular expression of "date" using Python
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
How to extract the desired character string from a line 4 commands
How to intercept or tamper with the SSL communication of the actual iOS device by a proxy
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
[NNabla] How to remove the middle tier of a pre-built network
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
How to crop the lower right part of the image with Python OpenCV
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[NNabla] How to add a quantization layer to the middle layer of a trained model
How to put a line number at the beginning of a CSV file
I want to color a part of an Excel string in Python
How to create a wrapper that preserves the signature of the function to wrap
[Introduction to Python] How to write a character string with the format function
How to play a video while watching the number of frames (Mac)
I tried to verify the result of A / B test by chi-square test
How to sort by specifying a column in the Python Numpy array.
How to check the version of Django
[Python] How to invert a character string
How to get the "name" of a field whose value is limited by the choice attribute in Django's model
How to pass the execution result of a shell command in a list in Python
How to mention a user group in slack notification, how to check the id of the user group
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
Divides the character string by the specified number of characters. In Ruby and Python.
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
How to count the number of elements in Django and output to a template
How to access the contents of a Linux disk on a Mac (but read-only)
Use Pillow to make the image transparent and overlay only part of it
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
[Numpy, scipy] How to calculate the square root of a semi-fixed definite matrix
Paste a link to the data point of the graph created by jupyterlab & matplotlib
How to make a Raspberry Pi that speaks the tweets of the specified user
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
Try installing only the core part of Ubuntu
A simple example of how to use ArgumentParser
How to find the area of the Voronoi diagram
How to embed a variable in a python string
How to write regular expression patterns in Linux
How to erase the characters output by Python
[Note] How to create a Ruby development environment