Proper regex that i couldn't reach - regex

^http(?:s)?:\/\/(?:www\.|events\.)?(?:v\.)?(?:(?:youtube)|(?:youku))\.\w{2,}\/(?:(?:\d{4}\/[^\/]+\/api\/video-files\.php\?\w+=\w+|watch\?(?=[^?]*v=?\-?\w+)(?:[^\s?,^\&?]+)?)|(?:v_show\/id_(?:\w{10,})(?:\.html)?))$
I've such a regex that fits according to strings below :
http://v.youku.com/v_show/id_XODQxOTg0ODg0.html
http://www.youtube.com/watch?v=n66NLBbQ53w
http://events.youku.com/2014/misc/api/video-files.php?vid=XMjg3MzQ5NTg4
But I need to manage the last one to accept all the strings after 'vid='
for example this needs to be accessible also :
http://events.youku.com/2015/misc/api/video-files.php?vid=XMTI2MDEyNzQ0OA==
I've tried to add other | for this part such as \w+=\w+== but it didn't work...
Anyone please help me to accept the strings after the '=' sign ? \w+ doesn't work i think...

https://regex101.com/r/mR5cU3/1
Here there is:
I juste change your regexp adding (?:==)?
^http(?:s)?://(?:www.|events.)?(?:v.)?(?:(?:youtube)|(?:youku)).\w{2,}/(?:(?:\d{4}/[^/]+/api/video-files.php\?\w+=\w+(?:==)?|watch\?(?=[^?]*v=?-?\w+)(?:[^\s?,^\&?]+)?)|(?:v_show/id_(?:\w{10,})(?:.html)?))$
Solution :
^http(?:s)?:\/\/(?:www\.|events\.)?(?:v\.)?(?:(?:youtube)|(?:youku))\.\w{2,}\/(?:(?:\d{4}\/[^\/]+\/api\/video-files\.php\?\w+=\w+(?:==)?|watch\?(?=[^?]*v=?\-?\w+)(?:[^\s?,^\&?]+)?)|(?:v_show\/id_(?:\w{10,})(?:\.html)?))$

Related

Regex find a specific character zero or one or multiple time in a string

I'm upgrading a Symfony app with VSCode and I have numerous occurences of this kind of string :
#Template("Area:Local:delete.html.twig")
or
#Template("Group:add.html.twig")
In this case, I want to replace all the : with / to have :
#Template("Area/Local/delete.html.twig")
I can't think of doing it manually, so I was looking for a regular expression for a search/replace in the editor.
I've been toying with this fearsome beast without luck (i'm really dumb in regexp) :
#Template\("([:]*)"
#Template\("(.*?)"
#Template\("[a-zA-Z.-]{0,}[:]")
Still, I think there should be a "simple" regexp for this kind of standard replacement.
Anyone has any clue ? Thank you for any hint
You can use this regex with a capture group: (#Template.*):.
And replace with this $1/.
But you'll have to use replace all until there's no : left, that won't take long.
Just explaining a lit bit more, everything between the parenthesis is a capture group that we can reference later in replace field, if we had (tem)(pla)te, $1 would be tem and $2 would be pla
Regex!
You can use this regex #Template\("(.[^\(\:]*)?(?:\:)(.[^\(\:]*)?(?:\:)?(.[^\(\:]*)?"\) and replacement would simply be #Template\("$1/$2/$3
You can test it out at https://regex101.com/r/VfZHFa/2
Explanation: The linked site will give a better explanation than I can write here, and has test cases you can use.

Get a part of the second group

I'm having some difficulties with regex.
Here is an example of the string on which I'm doing regex:
This is some useless information (first;second;third;fourth;fifth;sixth) (seventh;eigth;ninth;tenth)
I am looking for a regex that will allows me to pick only one of the word in parenthesis, like 'ninth'. The word I need to pick depends on where I'm in my program, so I will just adapt the regex once I will know how to write it
The best I have found for the moment is : (?<=\()([^]]+?)(?=\)).*?
That allows me to match the whole content of the group between parenthesis.
Can someone help me please?
If the need is to match the contents between parenthesis given a variable
input parameter it can be done like this :
(?<=\()(?:(?![()]).)*?(?<=[(;])(ninth)(?=[);])(?:(?![()]).)*(?=\))
It is dynamically constructed by joining the three parts.
(?<=\()(?:(?![()]).)*?(?<=[(;])( + variable + )(?=[);])(?:(?![()]).)*(?=\))
https://regex101.com/r/6yxQyp/1
Where the variable is captured in group 1 if needed.
Thank you for your help.
I finally find a regex that allows me to get the value I wanted.
To reuse my example :
This is some useless information (first;second;third;fourth;fifth;sixth) (seventh;eigth;ninth;tenth)
The worlds 'first' and 'seventh' will always be there so if i want to get the value of second i will use :
\(first;(\w+)
to get ninth's value i will use :
\(seventh;\w+;(\w+)
Hope this will help someone else !
Have a good day :)

Selecting a character that's in a line twice with regex

I have a code setup like this:
'olderVehicleHdr' : '#cft("We can still find you the right tires. Tell us what you drive.")#'
,'weCanStillHelpYou' : '#cft("We can still help.")#'
,'name' : '#cft("Name")#'
I need to switch the ' ' over to " " but there is A LOT of these in this file. and I'd rather not do each one individually so I thought I would try a regex setup. However I don't want the single quotes around the cft and ending # sign to be selected, since they need to be single quotes in order for the double quotes to work.
For example: I want to take 'name' : '#cft("Name")#' and turn the single quotes around name and make them double quotes like so: "name" : '#cft("Name")#'
This regex will be used on sublime to search for the appropriate characters and replace them. So my question is, can you make a regex that only selects the single quotes at the begining of the line and replace them? Without disturbing the second set of single quotes?
I've tried some of the lookbehind methods but they don't seem to work either. Any help would be greatly appreciated. thanks!
if sublimetext3 regex engine is based on PCRE use this pattern
'#cft[^']*'(*SKIP)(*F)|'
Demo
After a bit of tinkering, I have actually found a regex combo which seems to do the trick:
(?<![a-z]|\(|\\)'(?=[a-z]|[A-Z]|[0-1])|(?<=[a-z]|[0-9])'(?!\)) It's a tad bit sloppy but I only need it very rarely, and it works to me needs for right now. Thank you for your time to try and help me solve this!

Regex Replacing characters with zero

I have the following string 3}HFB}4AF4}1 -M}1.
I have searched for this string using the regex :
([0-9])(\})([A-Z]{3})(\})([0-9][A-Z]{2}[0-9])(\})([0-9])(\s\-)([A-Z])(\})([0-9]).
I want to replace the } with 0. The Result I am looking for is 30HFB04AF401-M01, any assistance is appriciated. The tool I am using is Regex Buddy
A possible solution
Problem solved? In JavaScript at least :-)
"3}HFB}4AF4}1 -M}1".replace(/\}/g, "0");
// "30HFB04AF401 -M01"
I'm missing the point, right?
Assuming the language is JavaScript, we can write something like
"dfghj456783}HFB}4AF4}1 -M}1fghjkl8765".replace(/(?:[\d\w\s]+)([0-9]}[A-Z]{3}}[0-9][A-Z]{2}[0-9]}[0-9] -[A-Z]}[0-9])(?:[\d\w\s]+)/g, function () {
return arguments[1].replace(/}/g, "0");
});
What's possible in other languages though may be a different story.
Try the home of RegexBuddy for details.
So you've already got an expression to find instances of the string. Now you can either use groups to replace the characters, or you can use a separate regular expression over the string you found, simply replacing the } character within group(0) (which is the entire matched part of the input). I would certainly prefer the latter.
Fred seems to have created the replacement method for you already, so I won't repeat it here.
I have managed to find a solution to the formating in the JGSoft Lanugage used by Regex Buddy, thanks to all that provided suggestions that helped me channel my thoughts in the right direction.
Solution(I am still a beginner with Regex hence the syntax might not be efficent, but it does the job!!)
Using Group Names instead of Regex assiging groups with backreference and $ syntax.
Hence to replace 0 for } in the string 3}HFB}4AF4}1 -M}1 or any similar string. I used the following search and replacement syntax
Search : (?<Gp1>([0-9]))(?:})(?<Gp2>([A-Z]){3})(?:})(?<Gp3>([0-9])([A-Z]{2})([0-9]))(?:})(?<Gp4>([0-9]))(?:\s-)(?<Gp5>([A-Z]))(?:})(?<Gp6>[0-9])
Replace : ${Gp1}0${Gp2}0${Gp3}0${Gp4}-${Gp5}0${Gp6}
Result : 30HFB04AF401-M01

Alphanumeric With Punctuation Regex Expression

Hi I wonder whether someone may be able to help me please.
Firstly, I'm very new to this, so please bear with me.
I've been working through some of the many tutorials to try and come up with a preg match expression which allows:
Any alphanumeric,
Commas,
Full stops and,
Spaces
and I've come up with the following:
(!preg_match('/^[A-Za-z0-9 _.,]{5,35}$/', $nameofcontact))
In addition to this, because the field is optional, I would also like blank fields to pass the validation and it's this aspect that I'm having difficulty with.
I just wondered whether someone could perhaps provide some guidance on how I can allow the field to be blank in conjunction with the above parameters.
Many thanks and kind regards
Blank field : ^$
'Or' in Regex : |
Use this :
^$|^[A-Za-z0-9 _.,]{5,35}$