Regex search and replace YouTube shortcode - regex

I have a WordPress plugin where I recently used regex to search and replace, however, I am finding it tricky to remove a space that now remains.
I have a YouTube link, which I managed to use regex to add:
[embed](youtubelinkhere)[/embed]
However, there is a space that I can't get rid of before [/embed] that is causing some issues.
What should I search for to remove this space before [/embed] please?
Many thanks.

You'll have to escape the square brackets:
\s\[\/embed\]
Live Demo
https://regex101.com/r/fT4dP3/1

Related

Regex Finding URL's Within a string and replacing any word which ends at ".com"

I am trying to figure out how to look through a string and replace any word which ends at ".com" at the end with a valid url link.
for example:
"google.com has launces a .." will be replaced with
"<.a href='google.com'>google.com <./a> has launches .."
// I tried following code, but it only works for finding word which starts with "www."
data.rows[j].content = data.rows[j].content.replace(
/(^|<|\s)(www\..+?\..+?)(\s|>|$)/g,'$2'
)
First, I recommend learning how regex works. It's a very powerful tool that all developers should understand because it appears in many different programming languages.
Once you understand the basics, this should make more sense:
/(^|<|\b)(\S+?\.com)(\b|>|$)/g
Regex101 Demo - (for a breakdown of the regex, look in the top-right pane)
https://regex101.com/r/oF0mA9/2 should do the trick from the regex front.
You can follow this ways for ending com
(http(s?):)|([/|.|\w|\s])*\.(?:com)
OR
(?i)\.(com)$
OR
(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\.(?:com))(?:\?([^#]*))?(?:#(.*))?
Resource Link:
Regex to check if valid URL that ends in .jpg, .png, or .gif

Dreamweaver wildcard find and replace assistance

I was following this article about find and replace but have not been able to get it working for what I need to do...
What I need to find and replace it all links that contain ../external.html?link=something.html and replace them with #
This is what I tried to do in dreamweaver search
FIND:
<a href="../external.html?link=[^<]*"
REPLACE WITH
<a href="#"
But the problem is using that search does not find anything.
I figured it out from adobe documentation on the subject: http://help.adobe.com/en_US/dreamweaver/cs/using/WScbb6b82af5544594822510a94ae8d65-7c3ea.html#WScbb6b82af5544594822510a94ae8d65-7c1fa
The problem was that because the string i was search for had ? in it and the wildcard search is a regular expression. So to fix it I had to put a back slash before the ? character e.g: \?
This escapes the character and I was able to complete my task.
Hopefuly someone else who gets confused on this subject will find this helpful!

regular expression in excel for numbers before a slash

In the example below, I need to change everything before the final slash to jreviews/
so in the example below the first line would become
jreviews/159256_0907131531001639107_std.jpg
i am using open office find and replace tool, I see there is an option for regex but i dont know how to do this. How can I find and replace the img.agoda urls and everything thats a number and slash, and replace that with jreviews/ ?
but keeping the numbers after that final slash, because these are the filename.
http://img.agoda.net/hotelimages/159/159256/159256_0907131531001639107_std.jpg
http://img.agoda.net/hotelimages/161/161941/161941_1001051215002307125_std.jpg
http://img.agoda.net/hotelimages/288/288595/288595_111017161615_std.jpg
http://img.agoda.net/hotelimages/289/289890/289890_13081511070014319856_std.jpg
http://img.agoda.net/hotelimages/305/305075/305075_120427175058_std.jpg
http://img.agoda.net/hotelimages/305/305078/305078_120427175537_std.jpg
Regex seems like overkill, at least for your examples. Since they all have the same number of subfolders, a simple Find and Replace with wildcards works for me. Here's how I did it in Excel:
Just replace http://*/*/*/*/ with jreviews/.
Try this:
Replace the below match with "CustomName/"
^.+[/$]

RegEx filter links from a document

I am currently learning regex and I am trying to filter all links (eg: http://www.link.com/folder/file.html) from a document with notepad++. Actually I want to delete everything else so that in the end only the http links are listed.
So far I tried this : http\:\/\/www\.[a-zA-Z0-9\.\/\-]+
This gives me all links which is find, but how do I delete the remaining stuff so that in the end I have a neat list of all links?
If I try to replace it with nothing followed by \1, obviously the link will be deleted, but I want the exact opposite to have everything else deleted.
So it should be something like:
- find a string of numbers, letters and special signs until "http"
- delete what you found
- and keep searching for more numbers, letters ans special signs after "html"
- and delete that again
Any ideas? Thanks so much.
In Notepad++, in the Replace menu (CTRL+H) you can do the following:
Find: .*?(http\:\/\/www\.[a-zA-Z0-9\.\/\-]+)
Replace: $1\n
Options: check the Regular expression and the . matches newline
This will return you with a list of all your links. There are two issues though:
The regex you provided for matching URLs is far from being generic enough to match any URL. If it is working in your case, that's fine, else check this question.
It will leave the text after the last matched URL intact. You have to delete it manually.
The answer made previously by #psxls was a great help for me when I have wanted to perform a similar process.
However, this regex rule was written six years ago now: accordingly, I had to adjust / complete / update it in order it can properly work with the some recent links, because:
a lot of URL are now using HTTPS instead of HTTP protocol
many websites less use www as main subdomain
some links adds punctuation mark (which have to be preserved)
I finally reshuffle the search rule to .*?(https?\:\/\/[a-zA-Z0-9[:punct:]]+) and it worked correctly with the file I had.
Unfortunately, this seemingly simple task is going to be almost impossible to do in notepad++. The regex you would have to construct would be...horrible. It might not even be possible, but if it is, it's not worth it. I pretty much guarantee that.
However, all is not lost. There are other tools more suitable to this problem.
Really what you want is a tool that can search through an input file and print out a list of regex matches. The UNIX utility "grep" will do just that. Don't be scared off because it's a UNIX utility: you can get it for Windows:
http://gnuwin32.sourceforge.net/packages/grep.htm
The grep command line you'll want to use is this:
grep -o 'http:\/\/www.[a-zA-Z0-9./-]\+\?' <filename(s)>
(Where <filename(s)> are the name(s) of the files you want to search for URLs in.)
You might want to shake up your regex a little bit, too. The problems I see with that regex are that it doesn't handle URLs without the 'www' subdomain, and it won't handle secure links (which start with https). Maybe that's what you want, but if not, I would modify it thusly:
grep -o 'https\?:\/\/[a-zA-Z0-9./-]\+\?' <filename(s)>
Here are some things to note about these expressions:
Inside a character group, there's no need to quote metacharacters except for [ and (sometimes) -. I say sometimes because if you put the dash at the end, as I have above, it's no longer interpreted as a range operator.
The grep utility's syntax, annoyingly, is different than most regex implementations in that most of the metacharacters we're familiar with (?, +, etc.) must be escaped to be used, not the other way around. Which is why you see backslashes before the ? and + characters above.
Lastly, the repetition metacharacter in this expression (+) is greedy by default, which could cause problems. I made it lazy by appending a ? to it. The way you have your URL match formulated, it probably wouldn't have caused problems, but if you change your match to, say [^ ] instead of [a-zA-Z0-9./-], you would see URLs on the same line getting combined together.
I did this a different way.
Find everything up to the first/next (https or http) (then everything that comes next) up to (html or htm), then output just the '(https or http)(everything next) then (html or htm)' with a line feed/ carriage return after each.
So:
Find: .*?(https:|http:)(.*?)(html|htm)
Replace with: \1\2\3\r\n
Saves looking for all possible (incl non-generic) url matches.
You will need to manually remove any text after the last matched URL.
Can also be used to create url links:
Find: .*?(https:|http:)(.*?)(html|htm)
Replace: \1\2\3\r\n
or image links (jpg/jpeg/gif):
Find: .*?(https:|http:)(.*?)(jpeg|jpg|gif)
Replace: <img src="\1\2\3">\r\n
I know my answer won't be RegEx related, but here is another efficient way to get lines containing URLs.
This won't remove text around links like Toto mentioned in comments.
At least if there is nice pattern to all links, like https://.
CTRL+F => change tab to Mark
Insert https://
Tick Mark to bookmark.
Mark All.
Find => Bookmarks => Delete all lines without bookmark.
I hope someone who lands here in search of same problem will find my way more user-friendly.
You can still use RegEx to mark lines :)

Regex to match surrounding text

I need to replace "something[anything]" with "somethingElse(anything)", the tricky part is anything could be anything, and I don't want to replace that. Is it possible to do with regex?
P.S. In real scenario I'd be using PHPStorm to look / replace it. It would help me a lot to show me what to put exactly at the find window.
Thank you!
Idea: Capture 'anything' in a capturing group in regex. Then, back reference it in the replacement.
For example: search for something[([^]]*)\] and replace with somethingElse($1).