mod rewrite to match number at end of string - regex

I'm trying to write a bit mod rewrite regex and getting stuck.
I want to parse the following url structure to give variables to my script:
dogs/staffordshire-brown-hampshire-32
(32 is the variable I want to pass)..
index.php?type=dogs&id=$matches[1]
I have this so far which doesn't seem to work
dogs/([^-]*$)
Any ideas? Thanks

That will match the entire string after dogs if it doesn't contain a hyphen. Try:
dogs/.*?([0-9]+)$

Related

Terraform Replace - Regex Find and string Append

I need to use Terraform replace regex to pattern match and append.
So far I have been able to write this:
> replace("repo:company/example:environment:sandbox", "/(<=environment:)(.*)+(=:)/", "1_deplopy")
"repo:company/example:environment:sandbox"
Problem is it is appending in the wrong space
"repo:company/example:environment:sandbox"
It should be
"repo:company/example:environment:sandbox_deplopy"
Example of 3 types of variables, 2 of which will need to be altered
repo:company/example:environment:sandbox:job_workflow_ref:test.yaml
repo:company/example:environment:sandbox
repo:company/example:*
Is anyone able to help refine this to work?
Its appending at the start of the pattern, not at the end
replace("repo:company/example:environment:sandbox", "/(environment:*:)/", "_deplopy$1")
"repo:company/example:_deplopyenvironment:sandbox"
Thanks
> replace("repo:company/example:environment:sandbox", "/(environment:[^:]+)/", "$${1}_deplopy")

Regex for paginated URLs

I'm trying to work out a regex pattern to match URL's.
I currently have URL structures like so
walks/category/catname
walks/category/catname/P12
walks/category/catname/P24
I want to be able to match them and replace them so they end up like so
walks/catname
walks/catname/p2
walks/catname/p3
(where catname is a variable)
So far I had
/walks/category/(.*) to /walks/$1
Which seems to work for the first page of results, but obviously not for the paginated ones.
I'm ab bit stuck so any pointers would be very much appreciated.
Thanks!
Sort of like
Find walks/.*?(/catname.*)
Replace walks$1
Construct the regex to insert the catname variable
var regex = 'walks/.*?(/' + vCatname + '.*)'
before:
walks/category/catname
walks/category/catname/P12
walks/category/catname/P24
after:
walks/catname
walks/catname/P12
walks/catname/P24

Regex that matches a pattern only if string does not begin with 'N'

I need to put together a regex that matches a patter only if string does not begin with 'N'.
Here is my pattern so far [A-E]+[-+]?.
Now I want to make sure that it does not match something like:
N\A
NA
NB+
NB-
NCAB
This is for REGEXP_SUBSTR command in Oracle SQL DB
UPDATE
It looks like I should have been more specific, sorry
I want to extract from a string [A-E]+[-+]? but if the string also matches ^(N|n) then I want my regex to return nothing.
See examples below:
String Returns
N/A
F1/AAA AAA
NABC
FABC ABC
To match a character between A and E not preceded by N, you can use:
([^N]|^)[A-E]+
If you want to avoid fields that contains N[A-E] use a negation in your query using the pattern N[A-E] (in other words, use two predicates, this one to exclude NA and the first to find A)
To be more clear:
WHERE NOT REGEXP_LIKE(coln, 'N[A-E]') AND REGEXP_LIKE(coln, '[A-E]')
Ok I figured it out, I broadened the scope of the problem a little, I realized that I can also play with other parameters of REGEXP_SUBSTR in this case that I can have returned only second substring.
REGEXP_SUBSTR(field1, '^([^NA-D][^A-D]*)?([A-D]+[-+]?)',1,1,'i',2)
I still have to give you guys the credit, lot of good ideas that led me to here.
Just throw a [^N]? in front. That should do it.
OOPS...
That actually needs to include an " OR ^ "...
It should look like this:
([^N]|^)[A-E]+[-+]?
Sorry about that...It looks like the right answer already got posted anyway.

how to write regex to get the first level group

I have a string like this
env1,env2[data1,data2],env3
I want a regex to get the first groups
env1
env2[data1,data2]
env3
then I can write another regex(pcre) to parse data1,data2.
But I don't know how to parse the first level.
#Simon
Yes.
I want grap the env2[data1,data2] firstly.
If you need to capture the env + number + [everything that is not ]...], then you may use this regex:
(env\d+\[[^\]]+\])
https://regex101.com/r/fE9nF8/1

Parse This or This Using Regex

I simply can't figure this out and have been trying for awhile. I need a regex that will parse data in the following manner:
Lets say I've got an input in the following format:
www.google.com
www.google.com/
www.google.com/something
I need a regex that will parse the above three URLs (individually) to final result of:
www.google.com
www.google.com
www.google.com
However, the way it needs to match them, is based on the following:
Parse and return everything to the left of a "/" if one exists in the line
Parse and return the entire line if no "/" exists in the line
I'm new to regex, so while this may be simple, I can't figure it out.
Try the following regex:
[^/]*
Substiture /.* with nothing. It doesn't matter if there isn't a / at all since in this case the regex will not match.