Regex Reverse Search on a String - regex

I'm trying to extract the second last value in a string so I can return the value "Minute". I'm using Zapier which only allows regex.
Example string:
Week,Day,Hour,Another,Hour,Minute,Second
I've got a positive lookahead working below that returns the value "Second", however I cannot figure out how to apply an n-1 style operator to return the second-last string value "Minute".
My regex:
(?!^\,)[^,]*$
Try it here: https://regex101.com/r/mXVg6W/1
Any ideas?

You can use the following regex:
[^,]+(?=(?:,[^,]+){1}$)
It extracts the first non-comma values [^,]+ occurring after a comma, a sequence of non-comma values and the end of string (?:,[^,]+){1}$. If you want preceeding values, it's sufficient to increment the value inside of the braces (the amount of ,string to be passed over).
Check the demo here.

How about letting the dot consume and capture (no lookarounds).
.*,([^,]+),
See this demo at regex101
Obviously this only makes if at lesat two commas occure in the string.

Does this work? [\d+\,]{9}(\d)[\,\d]

Related

Regex that either matches a string OR returns a non empty value

I am writing Regex for a REST API where specific numeric values upto five decimal places are extracted by API. I am trying to write a regex that either matches alphanumeric value or returns a non empty value if no match is found. The alpha numeric values are in a lengthy string. A block of string example is below:
,"circulatingSupply":18687562,"totalSupply":18687562,"maxSupply":21000000,"marketCapDominance":50.5442,"
We need maxsupply numeric value so I successfully built the regex to extract the numeric value:
/\,\"maxSupply\"\:([0-9]+[.]?[0-9]{0,5})[0-9]*\,\"/
But the problem is that the substring may or may not be present. If the string is not present then the regex should return non empty value (not a NULL value or undefined value). I tried following but couldn't get a working regex as all are being rejected by the REST API:
/\,\"maxSupply\"\:([0-9]+[.]?[0-9]{0,5})[0-9]*\,\"|([ ])/
/(?:\,\"maxSupply\"\:([0-9]+[.]?[0-9]{0,5})[0-9]*\,\")?/
/\,\"maxSupply\"\:([0-9]+[.]?[0-9]{0,5})[0-9]*\,\"|( )/
/(?:\,\"maxSupply\"\:([0-9]+[.]?[0-9]{0,5})[0-9]*\,\"){0,1}/
I tried using OR operator but i think I am having a problem in Right Hand Side operand of OR operator. The required substring is necessarily in the middle of the large string so I guess ^ and $ will not be of any use. To emphasize, I repeat that if substring is not present then the regex should return non empty value say zero or a space character.
I found the solution from somewhere. I did the following thing and I got the desired result. It was all about capture group and OR operator placement.
$_ =~ /(?:\,\"maxSupply\"\:([0-9]+[.]?[0-9]{0,5}[0-9])*\,\"|(No Data))/

Remove string between 2 pattern using gawk regex only

Input:
secNm:ATA,_class:com.dddao.domaffin.summaggrfy.GddenericMohsg},{ttlRec:0,ttlVal:{:0}secNm:B2B,_class:com.xyz.dakjdain.sfffummary.GenericMo73hs}extra
secNm:ATA,_class:com.dddao.domaffin.summaggrfy.GddenericMohsg},{ttlRec:0,ttlVal:{:0}secNm:B2B,_class:com.xyz.dakjdain.sfffummary.GenericMo73hs
In above both the string I want to remove,
For String 1: parts which stars from ",_class" and ends at 1st occurrence "}"
For String 2: parts which stars from ",_class" till the end if if 1st condition fails.
Output:
secNm:ATA,{ttlRec:0,ttlVal:{:0}secNm:B2Bextra
secNm:ATA,{ttlRec:0,ttlVal:{:0}secNm:B2B
This type of pattern is present undefinable times in this above string.
I want simple want to remove those part.
I have written regex function gsub(/,_class(.*?)\}/,"",$0)
I want answer only using gawk regex function only no other method.
My above give function is having some issue and removing big part of the string.
Help me to correct my regex formula please.
Thanks in advance.
You may use a [^}] negated bracket expression to match any char but } since lazy quantifiers are not supported.
Besides, you do not even need a grouping construct here as you are not referring to the captured value here. You may remove ( and ) safely.
Use
/,_class[^}]*}/
Basically, this should be understood as:
,_class - match ,_class substring
[^}]* - 0 or more chars other than }
} - up to and including }.

How to get the queryparam vid from the url using regex

Help me with the regex, I am trying to get the vid value from the following url.
I tried with like the following but I am not sure with that:
[\&]{1}vid[\=][\d]*
Is that correct?
Use vid=(\d+) for numbers of IDs see regex
Try Your Regex on this place...
https://regex101.com/r/dX3hD4/1
The trick here is to match between two patterns of interest -
"vid="
"&"
Anything you capture between that is what you're after.
Hence use this:
"http://gorid.com/api.jsp?acs=123&vid=432&skey=asdasd-asdas-adsasd".match("vid=([^;]*)&")[1]
We're accessing the 2nd element of the match object because that contains the value.
In a JS/PHP type environment, you can match on something like this, where you just find anything alphanumeric is between vid= and the following &:
vv = str.match(/vid=(.+?)&/)[1];
HERE
If the value is always numeric, replace (.+?) with (\d+?)
The regex you wrote will not work because you are including the characters &vid= in the return value. To make sure the regex engine checks for the string &vid= but does not include it in the result you will need to use a lookbehind:
(?<=&vid=)([^&\r\n]+)
We use a positive lookbehind to find &vid= and then grab everything from that point until the next & sign or the end of the line.
For your second request, if you wish to verify that the content of vid is a valid number you need to specify that all the characters following &vid= should be digits and also include a positive lookahead that makes sure the next character after the digits is a & sign. The corresponding regular expression then becomes:
(?<=&vid=)([^\D]+)(?=&)

regex to find content from occurrance of word and not including the word until a char

I have the following string:
my-value=dingdong.example.org;another-value=a1.2xscf;generated-at=lala.example.com;orig-val=blahblah
and am trying to figure out how to capture just the values of the variable names.
I.e. if I search for string my-value, I want the output to return everything after the = and up to the ;.
In the above example this should output:
dingdong.example.org
The following regex
(.*?my-value=.*?);
gets the string I am looking for, but I've not figured out how to split after the =.
Put the opening parentheses after the = instead of the beginning of the pattern and you'll the value in the first capture group:
.*?my-value=(.*?);
Demo: https://regex101.com/r/LtCnpI/1

regex needed for parsing string

I am working with government measures and am required to parse a string that contains variable information based on delimiters that come from issuing bodies associated with the fda.
I am trying to retrieve the delimiter and the value after the delimiter. I have searched for hours to find a regex solution to retrieve both the delimiter and the value that follows it and, though there seems to be posts that handle this, the code found in the post haven't worked.
One of the major issues in this task is that the delimiters often have repeated characters. For instance: delimiters are used such as "=", "=,", "/=". In this case I would need to tell the difference between "=" and "=,".
Is there a regex that would handle all of this?
Here is an example of the string :
=/A9999XYZ=>100T0479&,1Blah
Notice the delimiters are:
"=/"
"=>'
"&,1"
Any help would be appreciated.
You can use a regex like this
(=/|=>|&,1)|(\w+)
Working demo
The idea is that the first group contains the delimiters and the 2nd group the content. I assume the content can be word characters (a to z and digits with underscore). You have then to grab the content of every capturing group.
You need to capture both the delimiter and the value as group 1 and 2 respectively.
If your values are all alphanumeric, use this:
(&,1|\W+)(\w+)
See live demo.
If your values can contain non-alphanumeric characters, it get complicated:
(=/|=>|=,|=|&,1)((?:.(?!=/|=>|=,|=|&,1))+.)
See live demo.
Code the delimiters longest first, eg "=," before "=", otherwise the alternation, which matches left to right, will match "=" and the comma will become part of the value.
This uses a negative look ahead to stop matching past the next delimiter.