Check if a word appears twice with regex [duplicate] - regex

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

Related

Grab a value from a string using Regex [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 2 years ago.
I have a link that I want to grab a specific character out of. This is the link in question
"https://example.com/example-myexample/from/MyHouse/to/48DFjjhf333333333/view"
I want to grab the following id 48DFjjhf333333333 from this link using a regular expression. In this link /to/ and /view never change. The only thing that ever changes is the id that you see 48DFjjhf333333333.
I've tried the following regular expression:
\/to(.*?)\/view
This grabs everything along with /to/ and /view as follows /to/48DFjjhf333333333/view. I want to grab just the ID itself 48DFjjhf333333333
You just need to access the matching group with [1]
console.log("https://example.com/example-myexample/from/MyHouse/to/48DFjjhf333333333/view".match(/\/to\/(.*?)\/view/)[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

How to end regex on first search [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I have the following string:
<center>DB Results:</center><center><b><br>ripster.ultima</b></center><center><b><br>raghav.jhavar</b></center></center></td></tr></table>
I want to extract "ripster.ultima" and "raghav.jhavar", so I used the following regex:
(?<=<center><b><br>)(.*)(?=<\/b><\/center>)
Now, this works in some cases, but not in all: https://regex101.com/r/imO8A8/4
In the link above, the first example is a single line. When I put in a single line, it does not select the individual strings. However, if its separated by a new line as seen in the second example, the strings I want are highlighted.
How do I get the strings in the first example?
You don’t need to group the first and the last patterns
<center><b><br>(.*?)<\/b><\/center>

Regex to make last letter capital [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 4 years ago.
SO i got regex to make first letter capital but i need now a regex to make the last letter caps, ive tried google searches and came up with nothing
Example of what im talking about
razor123
james333
firefire32923932
laser
Need them to turn into
razoR123
jameS333
firefirE32923932
laseR
Try this:
Find: ^([a-z]+)([a-z][^a-z]*)$
Replace with: \1\U\2

Matching multiple occurrences with regex [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'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