how to process double quotes in Regex? - regex

i want to use Regex in div at HTML in order to replace a certain string, this string is like :
age:7Refat"student" or it will be like age:7Refat , i'm using the following command that is ok with the second pattern:
$("#order_list").append($(this).text().replace(new RegExp("age:[0-9]+","g"),''));
but what if i want to use a general command for both patterns, the something is i don't know how to deal with the first pattern as it has double quotes"" , and i can't write:
$("#order_list").append($(this).text().replace(new RegExp("age:[0-9]+"[a-z]"","g"),''));
or
$("#order_list").append($(this).text().replace(new RegExp("price:[0-9]+[\"a-z\"]","g"),''));

Either escape the quotes like you did in your third example (but I think you put them in the wrong place):
new RegExp("price:[0-9]+\"[a-z]\"","g")
or (better) use a regex literal:
/price:[0-9]+"[a-z]"/g

You may try this
$("#order_list").append($(this).text().replace(new RegExp("age:[0-9]+\"[a-z]\"","g"));
instead of this:-
$("#order_list").append($(this).text().replace(new RegExp("age:[0-9]+","g"),''));

That finally works with me :
$("#order_list").append(($(this).text().replace(new RegExp("age:[0-9]+","g"),'')).replace(new RegExp("[a-zA-Z]+","g"),'').replace(new RegExp("\"+","g"),''));

Related

How to match all URLs until slash?

I need a RegEx for matching all of these URLs:
https://www.domain.tld/service?itm_pm=de:ncp:ctr:c1cn:0:0
https://www.domain.tld/service
https://www.domain.tld/service/
But not these one:
https://www.domain.tld/service/afdsasdaf
https://www.domain.tld/service/afdsasdaf/asdasd
I tried it with
https://www.domain.tld/service[^/]*
but it doesn't work
Mark the end of the string
Summary of changes:
I would work with a $ delimiter for "end of string"
A / usually needs to be escaped. This may be different based on your settings/language etc.
The . must be escaped as well, otherwise wwwwdomain.tld would be found
Let's use this one:
Solution with working example:
https:\/\/www\.domain\.tld\/service[^\/]*\/?$
You can play around with it here:
https://regex101.com/r/wm6Nit/1
If you want to allow https://www.domain.tld/service/ specifically, do that explicitly:
https://www.domain.tld/service(/?|[^/]*)$

Regex Python, Find Everything Inbetween Quotes after Keyword

I have strings that looks like this:
"Grand Theft Auto V (5)" border="0" src="/product_images/Gaming/Playstation4 Software/5026555416986_s.jpg" title="Grand... (the string continues for a while here)
I want to use regex to grab this: /product_images/Gaming/Playstation4 Software/5026555416986_s.jpg
Basically, everything in src="..."
At the moment I produce a list using re.findall(r'"([^"]*)"', line) and grab the appropriate one, but there's a lot of quotes in the full string and I'd like to be more efficient.
Can anyone help me put together an expression for this please?
Try with this
(?<=src=").+(?=" )
Use this as RE :
src="(.+?)"
This will return result as you want.
re.findall('src="(.+?)"', text_to_search_from)

regex to allow certain input values

I want to allow input values as A+,B+,A-,B- or 2 decimal values like 100.00, 90.0 like this
how to write regex for above input? simply I want to allow grades(A+,A-,B+,B-),decimal values (10.05,20.00).
The below regex will helpful to you:
[AB][+-]|\d{2}\.\d{2}
Description and Demo At: Demo
For what I am seeing, I would use this regex (I bet you can optimize it).
^([A-GOa-go][+-])|((\d{1,2}(?!\d)\.\d{2}|100\.00),(\d{1,2}(?!\d)\.\d{2}|100\.00))$
Here is the demo
Try this:
([AB][+-]|(100|\d{2})\.\d{2})
This, in my opinion, will work for what you are expecting
Online test : RegExr.com
EDIT :
Following what you are expecting for, i suggest you this regex :
^([AB][+-]|(100|\d{2})\.\d{2})$
Will match only if the entire string matches, and no longer return a 02.00 match for 102.00 (for example)

Need regex to strip away remaing part of a path

I am trying to write a regex which will strip away the rest of the path after a particular folder name.
If Input is:
/Repository/Framework/PITA/branches/ChangePack-6a7B6/core/src/Pita.x86.Interfaces/IDemoReader.cs
Output should be:
/Repository/Framework/PITA/branches/ChangePack-6a7B6
Some constrains:
ChangePack- will be followed change pack id which is a mix of numbers or alphabets a-z or A-Z only in any order. And there is no limit on length of change pack id.
ChangePack- is a constant. It will always be there.
And the text before the ChangePack can also change. Like it can also be:
/Repository/Demo1/Demo2/4.3//PITA/branches/ChangePack-6a7B6/core/src/Pita.x86.Interfaces
My regex-fu is bad. What I have come up with till now is:
^(.*?)\-6a7B6
I need to make this generic.
Any help will be much appreciated.
Below regex can do the trick.
^(.*?ChangePack-[\w]+)
Input:
/Repository/Framework/PITA/branches/ChangePack-6a7B6/core/src/Pita.x86.Interfaces/IDemoReader.cs
/Repository/Demo1/Demo2/4.3//PITA/branches/ChangePack-6a7B6/core/src/Pita.x86.Interfaces
Output:
/Repository/Framework/PITA/branches/ChangePack-6a7B6
/Repository/Demo1/Demo2/4.3//PITA/branches/ChangePack-6a7B6
Check out the live regex demo here.
^(.*?ChangePack-[a-zA-Z0-9]+)
Try this.Instead of replace grab the match $1 or \1.See demo.
https://regex101.com/r/iY3eK8/17
Will you always have '/Repository/Framework/PITA/branches/' at the beginning? If so, this will do the trick:
/Repository/Framework/PITA/branches/\w+-\w*
Instead of regex you could can use split and join functions. Example python:
path = "/a/b/c/d/e"
folders = path.split("/")
newpath = "/".join(folders[:3]) #trims off everything from the third folder over
print(newpath) #prints "/a/b"
If you really want regex, try something like ^.*\/folder\/ where folder is the name of the directory you want to match.

golang regex to find urls in a string

I am tring to find all links in a string and then hyperlink them
like this js lib https://github.com/bryanwoods/autolink-js
i tried to use alot of regex but i always got too many errors
http://play.golang.org/p/iQiccXvFiB
i don't know if go has a different regex syntax
so, what regex that works in go that is good to match urls in strings
thanks
You can use xurls:
import "mvdan.cc/xurls"
func main() {
xurls.Relaxed().FindString("Do gophers live in golang.org?")
// "golang.org"
xurls.Relaxed().FindAllString("foo.com is http://foo.com/.", -1)
// ["foo.com", "http://foo.com/"]
xurls.Strict().FindAllString("foo.com is http://foo.com/.", -1)
// ["http://foo.com/"]
}
Use back-ticks instead of double-quotes for your string literals. Back-slashes inside double-quotes start escape sequences, which you don't need/want for this use case.
Additionally, how did you expect this to work?
"$0"