Matching multiple occurrences with regex [duplicate] - regex

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I'm building an extractor in Graylog to pull tac_plus syslog data.
I have a log:
<70>Oct 13 10:10:05 auth tac_plus[17354]: 2015-10-13 10:10:05 -0500#01110.10.89.1#011jmartinez#011tty132#01110.10.1.27#011stop#011task_id=146#011timezone=CDT#011service=shell#011start_time=1444747732#011priv-lvl=15#011cmd=show running-config <cr>
I want to extract the indvidual statements between the #011 markers. I was able to get the first section, the IP with:
(?<=#011)(.*?)(?=#011)
Now I want to extract the 'jmartinez'. I'm trying:
#011.*?#011(.*)(#011)
but it matches:
jmartinez#011tty132#01110.10.1.27#011stop#011task_id=146#011timezone=CDT#011service=shell#011start_time=1444747732#011priv-lvl=15
if i do:
#011.*?#011(.*)(#011tty)
it seems to work but i'd rather it not rely on seeing #011tty because it might be something else in another message.
what about the next one? how can I extract tty132, 10.10.1.27, stop, task_id=146, etc
any help would be greatly appreciated!

The simple answer is to use a reluctant quantifier (just like your working IP capture):
#011.*?#011(.*?)#011
But I would go further and capture all groups at once, eg:
#011(.*?)#011(.*?)#011(.*?)#011(.*?)#011(.*?)#011(.*?)#011

Related

Regex pattern to find text either of two URLs [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Closed 2 years ago.
I have the following regex:
(?<=unit/)(.*)(?=/)
I use it to get a page name between unit and the guid.
www.page.com/module/unit/design/6e8c1d2b-34vc-4b5c-71af-8b0f93b01b60?sid=5fh6y172-31d9-4r6b-9a15-daed6c541162&culture=en-US
Now i am receiving an URL like this, with /load before the sid.
www.page.com/module/unit/design/6e8c1d2b-34vc-4b5c-71af-8b0f93b01b60/load?sid=5fh6y172-31d9-4r6b-9a15-daed6c541162&culture=en-US
so i am getting this result from the regex
design/6e8c1d2b-34vc-4b5c-71af-8b0f93b01b60
How can i change the regex in order to get just design, moreover how can i create a regex that find design either or both URLs?
Instead of .* use [^/]* to match any sequence of characters not including /. This will get everything until the next /.
(?<=unit/)[^/]*

Replacing a certain number of characters after a match in regular expression [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I want to find any instance of
\/Network\/Cambric Corporate Center\/
and replace the next 8 characters with nothing.
So something like this
\/Network\/Cambric Corporate Center\/1164.DEV1164
Turns into this
\/Network\/Cambric Corporate Center\/1164
I'm working with a baked in replace regular expression visual programming piece, and this is my first post to here ever so please ask if more info is needed. I basically just need it to look like this
\/Network\/Cambric Corporate Center\/1164
if there is another solution without having to use replace
It is for a frequently updated mass source of data that I need to edit to make more compatible with arrays
Try (\/Network\/Cambric Corporate Center\/).{8} and replace with $1 to keep the first group but not anything else.
Here's the regex with the replacement: https://regex101.com/r/F4Y4VD/1

Find two QnA maker link handler and assign each found regex an html <a> tag [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I want a regex for the following string:
For more: [Click here](https://www.google.ca) and [click me](https://www.google.com)
For that My current regex is as follows.
/\[.*\]\((((https?\:\/\/)|(www\.))(\S+))/ig
Give me the regex with that I can find two different links in the same line.
Right now I am finding 1 combined regex for both of them.
My guess is that we have URLs in (), which we can use an expression similar to, if that'd be the case:
\((https?[^\s]+)\)
with a capturing group, where our desired outputs are.
Demo 1
For capturing the [], we would just expand our expression:
(\[.+?])\((https?[^\s]+)\) //$ sign removed
Demo 2

Regex help to filter only 720p [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I'd like to setup auto download of some Anime using an RSS feed, but only 720p versions. The format never changes and it always looks like below.
[Blahblah] Blahepisode - 12 [720p].mkv
Here is the regex I have come up with but cannot get to work properly.
/.\+[720p]+/g
Any help would be appreciated!
Assuming you have lines that look like your example, it will be mached with the following Regex:
.+(?:\[720p\].mkv)
It maches one or more chacacters at start, followed by '[720p].mkv'.
Note that the Square brackets are escaped to '\[' and '\]', otherwise they have special meaning.
if you only need '[720p]' then you can use:
\[720p\]

Check if a word appears twice with regex [duplicate]

This question already has answers here:
Check if word exists twice? (Regex) [closed]
(5 answers)
Closed 4 years ago.
I want to check with regex if the word link appears twice in a sentence.
The sentence goes: "This link is invalid. Try the link below." --> here I need the regex to check if "link" appears twice.
This is my first time trying regex and I only understood the grouping so far like \b(link)\b. With this I can check if the word link even appears but I need to know if it appears twice.
How can I achieve this?
Try this one:
\b(link)\b(?=.*\b\1\b)
This gives you the first occurrence of the duplicate word.
Tested it with your sentence here: https://regex101.com/r/2zGSKj/2
A shorter solution without groups would simply be
\blink\b.*\blink\b
Tested it here: https://regex101.com/r/2zGSKj/3