How can I improve this RE to not end at "('"? [duplicate] - regex

This question already has answers here:
regular expression for finding 'href' value of a <a> link
(9 answers)
Closed 2 years ago.
I have this regex
href=["'](.*?)["']
And I want to match this entire string but it only matches up to (' and does not include explore
href="javascript:openurl('/Xplore/accessinfo.jsp')"
It also has to match
href="/iel5/4235/4079606/04079617.pdf?tp=&arnumber=4079617&isnumber=4079606"
The first link is the only special case, I have been able to match all other cases with the regex I have provided, I just want to somehow exclude the ' in the middle of the first string.

What you could do is have a positive lookahead define the end of the string:
^href=("|').*?(?=\1)\1$
That way, no matter if its a single or double quote, the second capture group will run till it finds the same single or double quote.

Related

Adding words to a character class in Regex [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 2 years ago.
I am currently using the following character class:
[^\)\(] in my regex
I want to add the word 'hello' to this class so it is also not matched in my string.
I have tried
[^\)\((hello)]
but it does not work.
What can I do?
One typical way you would enforce that hello does not appear would be to use a negative lookahead, e.g.
^(?!.*hello)[^t()]+$
If you only wanted to exclude hello when it appears as a bona fide word, then surround it with word boundaries in the lookahead:
^(?!.*\bhello\b)[^t()]+$

match any character behind / (front shash) [duplicate]

This question already has answers here:
Regex: matching up to the first occurrence of a character
(14 answers)
Closed 4 years ago.
can someone please quickly help me with regularexpression to match any character and group it which is coming behind front shash:
ae-app001/UK/Server/company
in this i want to match ae-app001 only.
the format remains the same except that we dont know how many slash might be there,
sometimes the string might be just :
ae-app001/UK/Server
so i need a generic regex which will match string1 in the below:
string1/string2/string3/string4
string1/string2/string3
etc..
/^([^\/]+)/ Demo
But what if there's no slashes? Is it ok to capture whole string?

Regex substitution: find double quotes not following by specific character [duplicate]

This question already has an answer here:
Regex Match a character which is not followed by another specific character
(1 answer)
Closed 4 years ago.
I have the following situation:
3" a
3":a
3",a
3"a
3"2
3"A
I need to find a replace a double quote with space every time the double quote is not following by : or ,.
So, for my case the expected results will be:
3 a
3":a
3",a
3 a
3 2
3 A
Any idea how write this logic using regex?
Regards,
You can use a negative lookahead A(?!B) for that. It matches an expression A that is not followed by expression B.
The replacement of the matches with spaces will depend on the used language.
"(?![:,])
Applied to your examples: https://regex101.com/r/UiPlaC/2
If you want to handle the case 3" a without having multiple spaces, just include one (or even more?) optional spaces in the match.
"(?![:,])\ ?
See here for more information:
Regex lookahead, lookbehind and atomic groups
https://www.regular-expressions.info/lookaround.html

how to use regex to add leading zero [duplicate]

This question already has answers here:
Using regex to add leading zeroes
(13 answers)
Closed 5 years ago.
Question - what is the shortest form of regex to add a leading zero into a found pattern?
I want to add a leading zero to any number that matches this regex pattern
[(][0-9][0-9][0-9][0-9][-][0-9][0-9][0-9][0-9][-][0-9][0-9][)]
I am using Notepad++.
In Notepad++ Regex Replace, you use $n to represent the nth capture group for replacement:
Search for: [(]([0-9][0-9][0-9][0-9][-][0-9][0-9][0-9][0-9][-][0-9][0-9])[)]
Replace with: (0$1)

Regex for string containing one string, but not another [duplicate]

This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Closed 3 years ago.
Have regex in our project that matches any url that contains the string
"/pdf/":
(.+)/pdf/.+
Need to modify it so that it won't match urls that also contain "help"
Example:
Shouldn't match: "/dealer/help/us/en/pdf/simple.pdf"
Should match: "/dealer/us/en/pdf/simple.pdf"
If lookarounds are supported, this is very easy to achieve:
(?=.*/pdf/)(?!.*help)(.+)
See a demo on regex101.com.
(?:^|\s)((?:[^h ]|h(?!elp))+\/pdf\/\S*)(?:$|\s)
First thing is match either a space or the start of a line
(?:^|\s)
Then we match anything that is not a or h OR any h that does not have elp behind it, one or more times +, until we find a /pdf/, then match non-space characters \S any number of times *.
((?:[^h ]|h(?!elp))+\/pdf\/\S*)
If we want to detect help after the /pdf/, we can duplicate matching from the start.
((?:[^h ]|h(?!elp))+\/pdf\/(?:[^h ]|h(?!elp))+)
Finally, we match a or end line/string ($)
(?:$|\s)
The full match will include leading/trailing spaces, and should be stripped. If you use capture group 1, you don't need to strip the ends.
Example on regex101