Regex Negative Lookahead with numbers - regex

What I want to do is select everything except [0-9]{4,7}. I know negative lookaheads so I tried this: (?![0-9]{4,7}) but the result was strange. Any ideas to get this?
For example,
Sample text for my question!
2020 0316 or 200316
fdsa2020asdf
Desired
Sample text for my question!
or
fdsaasdf

This matches the desired parts of all your test cases:
\D{2,}
See live demo.
I added {2,} only to avoid matching the space between 2020 and 0316. If you're ok with matching that, you can remove {2,}.

Related

Regex Capture Middle Value

I would like to ask for your help...
I have this string where I have to get the 4.75. I've tried many regex expression but I could not get it to work and been through browsing lots of examples as well.
Regexr Image
Loan Amount Interest Rate
$336,550 4.75 %
So far, below is my current expression
1. (?<=Interest Rate\s*\n*)([^\s]+).+(?=%)
I'm getting the $336,550 4.75
2. ([^\s]+).(?=%)
Resulted into multiple output. In my entire text, which I can't share, there are also other data that is in %.
I am only after the 4.75. I know I can just select the first match via code (i guess) but for now it is not an option.
Thanks in advance!
I've tried different regex expression
You just need to extract "4.75 %" ?
Try this:
(?<=Interest Rate\n\n\$\d{3},\d{3}\s)(\d{1,5}\.\d{1,5}\s%)
Since your regex with variable length patterns inside lookbehind works, you can use the following .NET compliant regex:
(?<=Interest Rate\s+\S+\s+)(\S+)(?=\s*%)
See the regex demo.
Details:
(?<=Interest Rate\s+\S+\s+) - a positive lookbehind that requires Interest Rate, one or more whitespaces, one or more non-whitespaces and again one or more whitespaces immediately to the left of the current location
(\S+) - Group 1: one or more non-whitespace chars
(?=\s*%) - a positive lookahead that requires zero or more whitespaces and then a % char immediately to the right of the current location.
Hi Please try this.
[0-9]+.[0-9]+

Regex to find combination of lines not containing a string

I'm trying to find the correct regex that, within this input:
#Tag-1234
Scenario:
Blabla
Scenario:
Blabla
#Tag-1234
Scenario:
Blabla
Will select only the second one (the one without a tag).
So far I tried something like (?!#Tag-\d{4})\nScenario, but it's not doing the trick.
Can anyone throw some light into this?
I'm doing my tests on regex101 -> https://regex101.com/r/msDHKf/1
Thanks
It seems I was missing the \n before the tag that would concatenate to the search, so the regex would be
\n(?!#Tag-\d{4})\nScenario
So close!
In the pattern (?!#Tag-\d{4})\nScenario the lookahead can be removed as it is directly followed by matching \nScenario so the assertion will always be true.
If there should be no tag before Scenario but a newline, you can just match 2 newlines and then Scenario
\n\nScenario\b
See a regex demo.

Regex inverse match for the whole line

I've been trying to get this to work for an hour now but can't seem to do it, neither with the help of SO articles, or Regex101.com.
Have have some data and would like to return the lines that does not contain "/Common/http". Example data:
/Common/http and /Common/Another
/Common/http-mymon
/Common/another /Common/http
another line
The result I am looking for is:
/Common/http-mymon
another line
Any regex I use must match the whole line or it fails in the engine I use (https://github.com/jayway/JsonPath). This means that http would not work, but .*http.* would.
Hope this is fairly clear?
/Patrik
You can use a negative lookahead regex like this:
^(?:.*?/Common/http-mymon|(?!.*/Common/http)).*$
RegEx Demo
Update:
As per comment below OP wants to exclude /Common/http followed by / or a whitespace. In that case try this regex:
^(?!.*/Common/http(?:/|\s)).*$
RegEx Demo 2
I think you can go with the following regex
^(?!.*\/Common\/http(?:\s))(.*?)$
This one is checking for no /Common/http in front before space using negative lookahead (?!.*\/Common\/http(?:\s))

regex optional part in prefix, but do not include it in matches if it present

Problem is easier to be seen in code then described I got following regex
(?<=First(Second)?)\w{5}
and following sample data
FirstSecondText1
FirstText2
I only want matches Text1 & Text2 , I get 3 though, Secon is added, and I don't want that.
Played around, cant seem to get it to work.
You need an additional negative lookahead:
(?<=First(Second)?)(?!Second)\w{5}
If you want to avoid using Second twice, you could do it without lookaround and take the result of the first capturing group:
First(?:Second)?(\w{5})
You can try this regex (?<=First(Second)?)\w{5}$. All you have to do is to add a $ in the end so that the regex would not match the text Secon. You can use this as long as you are sure of the pattern that comes at the end of the input text. In this case it is \w{5}$

How to write regex pattern to match AAAA00000000-0000?

I need a regex to match a string in any of these formats:
"AAAA00000000"
"AAAA00000000-0000"
"0000000000"
I've got the first and third pattern right, this is what I came up with
^(([a-zA-Z]{4}[0-9]{8})|([0-9]{10}))$
I can't get that working to include the second pattern.
^[a-zA-Z]{4}[0-9]{8}(-[0-9]{4})?$
That is, XXXXnnnnnnnn and an optional -nnnn part.
XXXXnnnnnnnn
XXXXnnnnnnnn-nnnn
You can leave out the outermost parenthesis as this group equals the entire match (capturing group 0).
EDIT
Update to match nnnnnnnnnn, too:
^[0-9]{10}|[a-zA-Z]{4}[0-9]{8}(-[0-9]{4})?$
Matches:
nnnnnnnnnn
XXXXnnnnnnnn
XXXXnnnnnnnn-nnnn
EDIT #2
In response to comment, this is the shortest / most readable I'm able to cook up:
^[0-9]{10}|[a-zA-Z]{4}[0-9]{8}(-[0-9]{4}|)$
Same characteristics as immediately above.
You are missing the "-" of the second format...
^\w{4}\d{8}(-\d{4})?$
Fixed a typo
^[a-zA-Z]{4}([0-9]{8}|[0-9]{8}\-[0-9]{4})$