I have problem with removing params from url starting on ":". I have example path like:
/foo/:some_id/bar/:id
I would like to archive following result:
/foo/bar
I tried to create some regex. I figured out this:
\/:.*?\/
But this one removes :some_id but still It leaves :id part. Can you tell me how I can modify my regex to remove all params?
Your regex requires a / to be present at the end. You cannot just remove the / from the regex since .*? won't match anything then. Use a negated character class:
\/:[^\/]+
See the regex demo
Pattern details:
\/: - matches a literal /:
[^\/]+ - matches 1+ characters other than / as [^...] defines a negated character class matching all characters but those defined in the class.
Related
I am trying to match a word with regex. for example, I want to match only first 2 folders in below string
/folder1/folder2/filder3/folder4/folder5
I wrote a below regex to match first two folders but it matches everything till /folder5 but I wanted to match only till /folder2
/(\w.+){2}
I guess .+ matches everything. Any idea how to handle this?
You can use
^/[^/]+/[^/]+
^(?:/[^/]+){2}
Or, if you need to escape slashes:
^\/[^\/]+\/[^\/]+
^(?:\/[^\/]+){2}
See the regex demo. [^/] is a negated character class that matches any char other than a / char.
I want a regular expression to match on paths containing '/food/' but not '/food/api/':
http://example.com/food/api/pasta?sauce=true
Right now I'm using this:
/^((?!\/food\/api\/).)*$/
The problem with this is it matches ANY path that doesn't contain '/food/api/'
Behavior I want to achieve:
REGEX MATCHES
example.com/food/
example.com/food/meals
REGEX IGNORES
example.com/food/api/pasta?sauce=true
example.com/food/api/pasta
example.com/food/api/
example.com/meal
example.com/
Using a pattern like this ((?!\/food\/api\/).)* (a tempered greedy token solution) will match the whole line if it does not contain the sub string /food/api
As the quantifier is a * it will also match an empty line.
Instead, you can use an alternation to match until the first occurrence of a / followed by food or meal followed and a forward slash. After this slash, check that it is not followed by /api
^[^/]+/(?:food|meal)/(?!api/).*$
Regex demo
If the string can not contains spaces, you can exclude them using the negated character class [^/\s]+ and match \S* instead of .*
^[^/\s]+/(?:food|meal)/(?!api/)\S*$
Regex demo
I have a string and would like to match a part of it.
The string is Accept: multipart/mixedPrivacy: nonePAI: <sip:4168755400#1.1.1.238>From: <sip:4168755400#1.1.1.238>;tag=5430960946837208_c1b08.2.3.1602135087396.0_1237422_3895152To: <sip:4168755400#1.1.1.238>
I want to match PAI: <sip:4168755400#
the whitespace can be a word so i would like to use .* but if i used that it matches most of the string
The example on that link is showing what i'm matching if i use the whitespace instead of .*
(PAI: <sip:)((?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4})#
The example on that link is showing what i'm trying to achieve with .* but it should only match PAI: <sip:4168755400#
(PAI:.*<sip:)((?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4})#
I tried lookaround but failing.
Any idea?
thanks
Matching the single space can be updated by using a character class matching either a space or a word character and repeat that 1 or more times to match at least a single occurrence.
Note that you don't have to escape the spaces, and in both occasions you can use an optional character class matching either a space or hyphen [ -]?
If you want the match only, you can omit the 2 capturing groups if you want to.
(PAI:[ \w]+<sip:)((?:\([2-9]\d{2}\) ?|[2-9]\d{2}[ -]?)[2-9]\d{2}[- ]?\d{4})#
Regex demo
The regex should be like
PAI:.*?(<sip:.*?#)
Explanation:
PAI:.*? find the word PAI: and after the word it can be anything (.*) but ? is used to indicate that it should match as few as possible before it found the next expression.
(<sip:.*?#) capturing group that we want the result.
<sip:.*?# find <sip: and after the word it can be anything .*? before it found #.
Example
I have a string which I need to extract the "migration" value from (dynamic content).
The problem is that there are several patterns on the marked section.
Instead of defining 2 regex I would like to have it on single one.
(?i)Host: api-(.*?).A9net.io
(?i)Host: stt-(.*?).A9net.io
One pattern: Host: api-**migration**.A9net.io
Second pattern: Host: stt-**migration**.A9net.io
I need the migration value extracted
You might use an alternation to match either api or sst. Note to escape the dot to match it literally.
(?i)Host: (?:api|stt)-(.*?)\.A9net\.io
Regex demo
The (.*?) matches 0+ times which would also match when migration is not there. In that case you could use (.+?) instead to at least match 1 char.
If the migration value can not contain a dot, you might also use a negated character class to match 1+ times not a dot ([^.]+)
You could use this pattern: (?i)^Host: (?:stt|api)-([^.]+).A9net.io$
As already mentioned, alternation is key to your problem.
Additionally, it's recommended to use negated character class instead of lazy quantifier (such as +?) when possible. In this case it's [^.]+ - it matches one or more characters other than dot, so it will match untill first occurence of a dot, which is what you want when using lazu quantifier followed by dot.
Demo
I have an Ordernumber that looks like this: 1001-00001.2 but I want it without the extra .2
I tried to use the following in my code:
{$sArticle.ordernumber|regex_replace:"/'.'/\d":" "}
it didn't work because I don't know how I can use the dot.
Dot matches any single character. Try escaping it:
{$sArticle.ordernumber|regex_replace:"/\.\d+$/":""}
There's actually a few changes, here.
Everything was moved to be within the /.../ that mark a regex
The dot was escaped with \.
A quantifier (+) was added to \d so it matches one or more digits.
An anchor ($) is used to make sure it doesn't match anywhere but the end of the string.
The replacement is now an empty string