regex for finding only between brackets - regex

Given the below regex and text-
regex - #\{.*\}
text - "abc #{:abc :cde} dont-mtach #{:xyz :wqt} do-not do-not-not")
I would like to get only #{:abc :cde} #{:xyz :wqt} in the result. However the above also gives me dont-match in the result. Any ideas how I should modify the regex ?

#\{.*?\}
Make your * non greedy.Or simply use
#\{[^}]*\}
See demo

Related

Regex to remove everything after -i- (with -i-)

I was trying to find solution for my problem.
Input: prd-abcd-efgh-i-0dflnk55f5d45df
Output: prd-abcd-efgh
Tried Splunk Query : index=aws-* (host=prd-abcd-efgh*) | rex field=host "^(?<host>[^.]+)"| dedup host | stats count by host,methodPath
I want to remove everything comes after "-i-" using simple regex.I tried with regex "^(?[^.]+)" listed here
https://answers.splunk.com/answers/77101/extracting-selected-hosts-with-regex-regex-hosts-with-exceptions.html
Please help me to solve it.
replace(host, "(?<=-i-).*", "")
Example here: https://regex101.com/r/blcCcQ/2
This (?<=-i-) is a lookbehind
I have no knowledge of Splunk. but the normal way to do that would be to match the part you don't want and replace it with an empty string.
The regex for doing that could be:
-i-.*
Then replace the match with an empty string.
Something simple like this should work:
([a-z-]+)-i-.+
The first capture group will return only the part preceding -i-.

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.

regular expression : get super scripted text

I would like to get super scripted text via following html string.
testing to <sup>supers</sup>cript o<sup>n</sup>e
The result I would like to get is like below
supers
n
This is what I tried right now
But the result is not what I want.
<sup>supers
<sup>n
Could anyone give me suggestion please?
You can use lookbehind in your regex:
(?<=<sup>)[^<]*
Update Demo
Use this if there may be other HTML tags between <sup> and </sup>:
(?<=<sup>)(.*?)(?=<\/sup>)
Check the demo.
You were close, just not capturing your match:
Updated regex
(?:<sup>)([^<]*) I just added a capture group around your match
(?<=<sup>)([^<]*?)(?=<\/)
This should work.
See demo.
http://regex101.com/r/sA7pZ0/13

Get website regex from a website link

if I have a website like: www.google.com/en/my-page/anotherpage
how is it possible that with reg-ex to get: /en/my-page ? I am using this reg-ex in the IIS?
So far I have done something similar to this:
^(?:\\.|[^/\\])*/((?:\\.|[^/\\])*)/
but it is returning /en/my-page/ and I want it to return /en/my-page
In grep your regex is returning the string "www.google.com/en/". You can simply use the following regex if positive look behind is not mandatory :
(/[^/]+)+
You could use a look-ahead assertion to get rid of the last slash:
/\/.*(?=\/)/
This one should suit your needs:
^[^/]+(/.*)/[^/]+$
Visualization by Debuggex.
The output your looking for is in the first captured group.
Demo on RegExr.