This question already has answers here:
Is it possible to check if two groups are equal?
(2 answers)
Closed 5 years ago.
I'm trying to match iff two capture groups are the same. I could manually check after the match, but I'm wondering if there is a way I can do this in the expression itself.
My expression is (\d+)\/(\d+), so I only want to accept strings where the two numbers are equal. Is there a nice way to check this in the regular expression, or do I have to manually check groups after?
EDIT: This was marked a duplicate but the supposed duplicate question is not related and does not in any way answer my question...
You can use this one in python : \b(\d+)\/+\1\b
Demo
This is the same usecase as checking for doubled words
When editing text, doubled words such as "the the" easily creep in. Using the regex \b(\w+)\s+\1\b in your text editor, you can easily find them. To delete the second word, simply type in \1 as the replacement text and click the Replace button.
Source
I assume you don't have any other capture groups, based on that:
\b(\d+)\/(\1)\b
Regex Demo
Related
This question already has answers here:
Regex Match all characters between two strings
(16 answers)
RegEx match open tags except XHTML self-contained tags
(35 answers)
Closed 2 years ago.
I would like to mark all strings between 2 strings with the regular expression.
Example:
https://regex101.com/r/Etfpol/1
I want regular expression to mark follow text:
Solution changed from
Resolved Time changed
Updated By changed from
enter image description here
Thanks
You can use positive lookbehind and positive lookahead to check the tags.
(?<=<Name>Description<\/Name><Value>).*?(?=<\/Value>)
Match results
Solution changed from
Resolved Time changed
Updated By changed from
If you prefer not to use them, this will work as well, but the full match will include the strings before and after your desired string.
(?:<Name>Description<\/Name><Value>)(.*?)(?:<\/Value>)
This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 3 years ago.
Link Regex101
I am using (FD.*?)FD however I am missing every second expression - see regex. Any ideas?
The trailing "FD" in your regex matches the "FD" in the input that starts the next "item" in the input text, so the entire next "item" is skipped, until it finds another "FD".
In a case like this, instead of specify .* followed by the pattern that starts the next item, you typically want to specify anything not including a pattern of FD.
This question already has an answer here:
How to insert the whole matched text in the replacement in Perl?
(1 answer)
Closed 3 years ago.
I am trying to put every last element of each column of a csv into quotation marks by using regex in Visual Studio Code.
I am matching the string using [^,;]+$ and trying to replace it by using "$1".
After replacing, the strings are not in order anymore and some vanish.
Can anybody help me out here?
My csv is shaped like this:
SOME_ID,SOME_ID2,SOME_ID3,NUM,CODE
1234,100,1723,1,403
1235,101,1723,2,486
1236,101,1723,3,5822
To refer to capture you require to put the expression in a capture group (...)
Your regex is correct and simply needs to be put in the capturing group, ([^,]+)$ , should fix your search and replace.
As Wiktor suggested in the comments $& will refer to the whole match and that can also be used.
This question already has answers here:
How to negate specific word in regex? [duplicate]
(12 answers)
Closed 3 years ago.
I'm trying to scan all attributes from a database, searching for specific patterns and ignoring similar ones that I know should not match but I'm having some problems as in the below example:
Let's say I'm trying to find Customer Registration Numbers and one of my patterns is this:
.*CRN.*
Then I'm ignoring everything that are not CRNs (like currency and country name) like this:
(CRN)(?!CY|AME)
So far everything is working fine as look ahead is included in Javascript
The next step is to exclude things like SCRN (screen) for example but look behind (?<!S)(CRN)(?!CY|AME) doesn't work.
Is there any alternative?
Example inputs:
CREDIT_CARD
DISCARD
CARDINALITY
CARDNO
My Regex (?!.*DISCARD.*|.*CARDINALITY.*).*CARD.*
CARDINALITY was removed but DISCARD still being considered :(
The regex that you want is:
(?!\b(?:CARDINALITY|DISCARD)\b)(\b\w*CARD\w*\b)
It is important that you are testing the negative lookahead against the entire word and thus we are trying to match (\b\w*CARD\w*\b) rather than just CARD. The problem with the following regex:
(?!(?:CARDINALITY|DISCARD))CARD
is that with the case of DISCARD, when the scan is at the character position where CARD begins, we are past DIS and you would need a negative lookbehind condition to eliminate DISCARD from consideration. But when we are trying to match the complete word as we are in the regex I propose, we are still at the start of the word when we are applying the negative lookahead conditions.
Regex Demo (click on "RUN TESTS")
This question already has answers here:
RegExp exclusion, looking for a word not followed by another
(3 answers)
Closed 4 years ago.
I guess specifically this might be about a negative look ahead.
If i had a sentence in the form of:
this is the WORD i want and I want and this is the PHRASE I DONT WANT
is there a way to use just regex to match "WORD" but only not if "PHRASE" is present? My initial idea was a negative lookahead but that is only the immediate word following. I then tried using (?:\w+(?:\s*[\,\-\'\:\/]\s*|\s+)){0,3} and other similar tricks but this would match the words in-between and not the actual phrase. Not to mention the wonkiness of + in lookarounds. Then I thought about using a grouping like [^something] but i didnt know how to do that with full words without a lookaround. I then had the idea to nest lookarounds which i found out can happen, but that still gives me the root of the problem.
Can you skip words in the matching for a lookaround and if not how would i go about solving this issue?
Because if i nest using a lookbehind I still need to skip stuff to get to WORD in order to match it.
Assume the words are arbitrary in the sentence but the key word and the key phrase is something specific.
If I understand your question, you can try:
/(?!.*PHRASE I DONT WANT)WORD/
Demo