Regex to check for AND [duplicate] - regex

This question already has answers here:
regex for string starts with X and contains Y
(2 answers)
Closed 4 years ago.
I am trying to check whether a string starts from a particular word And also a particular substring occurs in it.
So lets say if I have a string like this:
SCSI\DISK&VEN_MICRON&PROD_1100\4&2096297&0&000200
I need to check whether this string has 4&2096297&0 and also it starts from SCSI
I did this : (^SCSI|\\4&2096297&0) . This is pretty much doing the work but it is also selecting the substring even if the string doesn't start from SCSI
Regex101 : Attempt
It is working as expected but the problem is it is selecting a substring even if the string doesn't start from SCSI.
Is it possible to put a AND condition to it.

You don't want to alternate with | - that will fulfill the regex if the left side or the right side of the | matches. You want to check that both conditions match. After the beginning of the string and SCSI, try repeating characters until you get to 4&2096297&0:
^SCSI.*4&2096297&0
https://regex101.com/r/xV0tHI/4
If you want to capture the already-hard-coded SCSI and 4&2096297&0, then put parentheses around them:
^(SCSI).*(4&2096297&0)

Related

How to select everything after the first white space using regex [duplicate]

This question already has answers here:
Regex to get all character to the right of first space?
(4 answers)
Closed 3 years ago.
I'm trying to get this via regex. I have a series of strings that all have the same general structure:
10-1:15 Build Support: (available)
A number, a space, a string following, a colon, and a string.
I need to extract the string portion after the first white space
Build Support: (available)
How can I regex this?
Side note: I'm on a Ruby project so I've been beating my head on https://rubular.com/.
This is something quick I came up on rubular:
https://rubular.com/r/tlfeSPy7zhfRN7
I tried matching with a group of non word characters and then capturing subsequent word characters. Match 2 should be what you're looking for.

REGEX - How can I return text between two strings [duplicate]

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.

Regex - How to exclude matches without look-behind? [duplicate]

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")

Regex pattern for String not ending in a sequence [duplicate]

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 4 years ago.
I am trying to find all strings not ending with the sequence -6X.
What i'm trying to do is compare the value of an xml tag with a sequence.
If it matches then do some stuff.
However I am unable to do so. I want to achieve this without any lookaheads or lookbehinds.
I've tried using .*[^-6X]
However this does not work for strings ending in - or -5 .
I want to ignore strings ending in -6X only and every other pattern should work.
Thanks in advance for your help
This matches Strings NOT ending with -6X:
.*?([^-]..|.[^6].|..[^X])$
https://regex101.com/r/omwkQ3/2

regular expression match if groups same [duplicate]

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