Regex * not working - regex

I have the string "abc.aspx?sdfsdfds;eter;yid=10". I want my regex to match the 10 part of that string.
I wrote the regex (abc.aspx?*[?;]yid=), but it is not matching my string.
The regex abc.aspx?yid=10;sdfsdf matches my string, and I used this instead of *.
Why does the first regex with * not match, but the second one without it does?
I also want ['test'?%] in this clause.
i.e before yid i want only ?,%,Test. for single character it works i.e $% but it is working for Test
I tried this (abc.aspx.*['Test'?%]yid=) but it consider ' as character rather i want to match whole Test word.

Escape the question mark for a literal question mark, and if yid can appear immediately after the question mark you need to make the entire intervening input optional:
abc.aspx\?(.*;)?yid=(\d+)
Your target is in group 1.
See live demo working with both of your sample inputs.

You have to escape ? and use .* instead of * :
abc.aspx\?.*[?;]?yid=(\d+)
Demo and Explanation

Related

Powershell - Parsing Cisco "Show run" text with regular expression [duplicate]

Suppose I have the following regex that matches a string with a semicolon at the end:
\".+\";
It will match any string except an empty one, like the one below:
"";
I tried using this:
\".+?\";
But that didn't work.
My question is, how can I make the .+ part of the, optional, so the user doesn't have to put any characters in the string?
To make the .+ optional, you could do:
\"(?:.+)?\";
(?:..) is called a non-capturing group. It only does the matching operation and it won't capture anything. Adding ? after the non-capturing group makes the whole non-capturing group optional.
Alternatively, you could do:
\".*?\";
.* would match any character zero or more times greedily. Adding ? after the * forces the regex engine to do a shortest possible match.
As an alternative:
\".*\";
Try it here: https://regex101.com/r/hbA01X/1

Check array syntax with Regex

I'm trying to create a regex that checks if a string is a valid path for Firestore document.
I will find a regex that testing if a string:
start with a char ^([a-z]{1})
after first char, there will be only letter/digit and/or a dot \w*(.?\w+){0,}
last chars in the string could be an index of an array (\[{1}\d+\]{1})?$
First and second points work well but the last group doesn't work. I test a string like data.images[11 and the regex return true.
first of all you can shorten some quantifiers in your regex:
{1} -> can be ignored completely
{0,} -> *
Your second part could be expressed like this, this will also support readability:
[\w.]* meaning: take any character inside the brackets 0 to n-times. The bracket expression also supports predefined classes, so we are using \w here. The dot INSIDE the brackets doesn't need to be escaped, it simply means the one character dot.
So your parts would be:
^([a-z])
[\w.]*
(\[\d+\])?$
I hope this helps. According to regexpal it matches data.images[11], but not data.images[11. Also it seems to support all your demands.
EDIT:
Your second part doesn't work because (like Asocia stated in the answer) you would need to escape the dot. The dot itself is a class meaning "any character" (depending on regex engine and settings sometimes even line breaks). As you mean the dot as a character you need to escape it.

RegEx for adding a zero between a dash and number [duplicate]

This question already has answers here:
Replacing digits immediately after a saved pattern
(2 answers)
Closed 3 years ago.
I want to find a way to add a leading zero "0" in front of numbers but BBEdit thinks it's substitute #10 Example:
Original string: Video 2-1: Title Goes Here
Desired result: Video 2-01: Title Goes Here
My find regex is: (-)(\d:)
My replace regex is: \10\2. The first substitute is NOT 10. I simply intend to replace first postion, then add a "0", then replace second position.
Kindly tell me how to tell BBEdit that I want to add a zero and that I don't mean 10th position.
If you simply need a number preceded by a dash, then I recommend using the regex lookbehind for this one.
Try this out:
(?<=-)(\d+:)
As seen here: regex101.com
It tells the regex that the match should be preceded by a dash -, and the - itself won't be matched!
You really don't need to capture hyphen in group1 (as it is a fixed string so no benefit capturing in group1 and replacing with \1) for replacement, instead just capture hyphen with digit using -(\d+:) and while replacing just use -0\1
Regex Demo
Also, there are other better ways to make the replacement where you don't need to deal with back references at all.
Another alternate solution is to use this look around based regex,
(?<=-)(?=\d+:)
and replace it with just 0 which will just insert a zero before the digit.
Regex Demo with lookaround
Another alternate solution when lookbehind is not supported (like in Javascript prior to EcmaScript2018), you can use a positive look ahead based solution. Basically match a hyphen - which is followed by digits and colon using this regex,
-(?=\d+:)
and replace it with -0
Regex Demo with only positive look ahead
Try \1\x30\2 as the replacement. \x30 is the hex escape for the 0 character, so the replacement is \1, then 0, then \2, and cannot be interpreted as \10 then 2. I don't know if BBEdit supports hex escapes in the replacement string though.
This expression might help you to do so, if Video 2- is a fixed input:
(Video 2-)(.+)
If you have other instances, you can add left boundary to this expression, maybe something similar to this:
([A-Za-z]+\s[0-9]+-)(.+)
Then, you can simply replace it with a leading zero after capturing group $1:
Graph
This graph shows how the expression would work:
If you wish, you can add additional boundaries to the expression.
Replacement
For replacing, you can simply use \U0030 or \x30 instead of zero, whichever your program might support, in between $1 and $2.

RegEx: How can I match all characters until the next match? [duplicate]

This question already has answers here:
Tempered Greedy Token - What is different about placing the dot before the negative lookahead?
(3 answers)
Closed 3 years ago.
I have a string like this:
Hello [#foo] how are you [#bar] more text
Ultimately I need to modify each instance of a substring matching /\[#.+?\]/, but I also need to modify each substring before/after the [#foo] and [#bar].
The following regex matches the substring before a [#.+], the [#.+] itself, then a substring after the [#.+] until the next character is followed by another [#.+].
(.*?)(\[(#.+?)\])((.(?!(\[#.+?\])))*)
So the first match is "Hello [#foo] how are you" and the second match is " [#bar] more text".
Note the space at the beginning of the second match. That's the problem. Is there a way to get the first match to include all characters right up to the next [#.+]?
My regex includes characters after the [#.+] that are not followed by an instance of [#.+], and I cannot see any way of getting it to include all characters until we are actually in another instance of [#.+].
I'm really interested in whether I'm missing something - it certainly feels like there should be a simpler way to capture the characters around a given match, or a simpler way to capture characters not part of a match...
You have this regex:
(.*?)(\[(#.+?)\])((.(?!(\[#.+?\])))*)
^
Look at that dot. It precedes a negative lookahead. It matches a unit of data only if negative lookahead is satisfied. If negative lookahead fails, dot won't match. This happens at a character before matching a \[#.+?\]. Hence the space character isn't included.
To include it you just change the order. Put the dot after negative lookahead is passed:
(.*?)(\[(#.+?)\])(((?!(\[#.+?\])).)*)
^
See live demo here
If I understand correctly, you want to separate your text into groups, each one having one instance of [#.+], and all of the text must be matched into a group.
Try (?:^.*?)?\[#.+?\].*?(?=\[|$).
This RegEx might help you to get those vars.
(?:\[#[A-Za-z0-9]+\])
You can also add any other char to [A-Za-z0-9] such as ., +, #:
`[A-Za-z0-9\.\+\#]`
and change it as you wish:
(?:\[#[A-Za-z0-9\.\+\#]+\])
x = 'Hello [#foo] how are you [#bar] more text'
out = re.search('((.*)(\[.*\])(.*))((\[.*\])(.*))',x)
After getting above output you can use groups method to access different groups:
out.group(1)
'Hello [#foo] how are you '
out.group(2)
'Hello '
out.group(3)
'[#foo]'
out.group(4)
' how are you '
out.group(5)
'[#bar] more text'
out.group(6)
'[#bar]'
out.group(7)
' more text'

Regex for deleting characters before a certain character?

I'm very new at regex, and to be completely honest it confounds me. I need to grab the string after a certain character is reached in said string. I figured the easiest way to do this would be using regex, however like I said I'm very new to it. Can anyone help me with this or point me in the right direction?
For instance:
I need to check the string "23444:thisstring" and save "thisstring" to a new string.
If this is your string:
I'm very new at regex, and to be completely honest it confounds me
and you want to grab everything after the first "c", then this regular expression will work:
/c(.*)/s
It will return this match in the first matched group:
"ompletely honest it confounds me"
Try it at the regex tester here: regex tester
Explanation:
The c is the character you are looking for
.* (in combination with /s) matches everything left
(.*) captures what .* matched, making it available in $1 and returned in list context.
Regex for deleting characters before a certain character!
You can use lookahead like this
.*(?=x)
where x is a particular character or word or string.{using characters like .,$,^,*,+ have special meaning in regex so don't forget to escape when using it within x}
EDIT
for your sample string it would be
.*(?=thisstring)
.* matches 0 to many characters till thisisstring
Here is a one-line solution for matching everything after "before"
print $1."\n" if "beforeafter" =~ m/before(.*)/;
Edit:
While using lookbehind is possible, it's not required. Grouping provides an easier solution.
To get the string before : in your example, you have to use [^:][^:]*:\(.*\). Notice that you should have at least one [^:] followed by any number of [^:]s followed by an actual :, the character you are searching for.