Regex how to match everything that doesn't follow the format with 3 digits followed by a dash then 4 digits? [duplicate] - regex

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 8 months ago.
\d{3}-\d{4}
This one matches something like 123-4567
So I change it to ^(?!(\d{3}-\d{4})). But it doesn't match anything.
I got a list of phone numbers like
123-4567
NOW-4-WAX
12 345 67 89
I would like to match anything except the format xxx-xxxx where x is a digit.

If you add .* to the end of your regex, it should match any line that does not start with a phone number in the 123-4567 format.
Demo on RegExr: ^(?!(\d{3}-\d{4})).*$
It's worth noting that if you have anything in your input file that is not a phone number at all, this will match those too.

Related

Google sheets to regexextract 4 digits only. Do not extract from longer number, say 1234 from 123456 [duplicate]

This question already has answers here:
regular expression to match exactly 5 digits
(5 answers)
Closed 27 days ago.
I have data where users enter 3 types of data:
random
4 digit / 6 digit
6 digit / 4 digit
I was looking for REGEXEXTRACT code in google sheets that will pick only 4 digit group. It will not be tricked by longer number, say "Article 1234124 Store 4444" it should return 4444 not 1234.
1234/1234567 --> 1234
1234567/5555 --> 5555
Article 1234124 Store 4444 -->4444
this would be a sample code, but google sheets does not support lookahead or lookbehind,
not a digit, 4 digits, not a digit:
=REGEXEXTRACT(A2,"(?<!\d)\d{4}(?!\d)")
thanks
You could use the following regex pattern:
(?:^|\D)(\d{4})(?:\D|$)
This pattern says to match:
(?:^|\D) Either a non digit or the start of the string
(\d{4}) A 4 digit number
(?:\D|$) Either a non digit or the end of the string
This ensures that we only match 4 digit numbers.
Sample code:
=REGEXEXTRACT(A2,"(?:^|\D)(\d{4})(?:\D|$)")

How to make regex that matches all possible episode numbers from a tv show file format? [duplicate]

This question already has answers here:
Regex for matching season and episode
(5 answers)
Closed 7 months ago.
I would like to create a regex expression that matches all possible episode numbering formats from a tv show file format.
I currently have this regex which matches most but not all of the list of examples.
(?:(?<=e)|(?<=episode)|(?<=episode[\.\s]))(\d{1,2})|((?<=-)\d{1,2})
The one it does not match is when there are two episodes directly after another e0102 should match 01 and 02.
You can find the regex example with test cases here
As per your comment, I went by following assumptions:
Episode numbers are never more than three digits long;
Episode strings will therefor have either 1-3 digits or 4 or 6 when its meant to be a range of episodes;
There is never an integer of 5 digits assuming the same padding would be used for both numbers in a range of episodes;
This would mean that lenght of either 4 or 6 digits needs to be split evenly.
Therefor, try the following:
e(?:pisode)?\s*(\d{1,3}(?!\d)|\d\d\d??)(?:-?e?(\d{1,3}))?(?!\d)
Here is an online demo. You'll notice I added some more samples to showecase the above assumptions.
e(?:pisode)?\s* - Match either 'e' or 'episode' with 0+ trailing whitespace characters;
(\d{1,3}(?!\d)|\d\d\d??) - A 1st capture group to catch 1-3 digits if not followed by any other digit or two digits;
(?:-?e?(\d{1,3}))? - An optional non-capture group with a nested 2nd capture group looking for optional hyphen and literal 'e' with trailing digits (1-3);
(?!\d) - There is no trailing digit left.

Regular expression: Should not start with 5 digits (or more) in a row [duplicate]

This question already has an answer here:
Regular expression for a string that does not start with a sequence
(1 answer)
Closed 1 year ago.
I need to generate a regular expression to validate that the string does not start with 5 digits.
NOT VALID: 12345testing123asd
VALID: 1234testing1234
testing12345
testing
I tried to get the first five chars ^.{0,5} but I do not know hot to add the restriction of \D to those first 5 chars
Also, I tried with this [^0-9][^0-9][^0-9][^0-9][^0-9] but I do not know how to do to include the strings that starts with 4 or less numbers
Could you please help me with this? I am a rookie :(
If your RegExp flavor of choice supports negative lookaheads, this pattern will match if the string is valid (does not start with 5 or more consecutive digits):
^(?!\d{5,})
Regex101
Matches:
1234testing1234
testing12345
testing
Does not match:
12345testing123asd

REGEX: match all numbers if a keyword exists [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm trying to find a regex which searches for every numbers if a keyword exists in the paragraph.
For example, if my keyword is something, with this paragraph:
20
30
abc
40
def
something
my regex should get 20, 30 and 40. But for this one:
50
60
xyz
it should get nothing.
Can you guys help me out to find a good regex. Thank you so much! I'm using PCRE
You can use this regex in single line (DOTALL) mode with a lookahead assertion:
(?s)\b\d+(?=.*\bsomething\b)
It will match numbers only when there is a word something ahead in input.
RegEx Demo
RegEx Details:
(?s): Enable single line mode so that dot also matches newlines
\b: Match a word boundary
\d+: Match 1+ digits
(?=.*\bsomething\b): Positive lookahead to assert that we have a word something ahead of us from current position

Regex to match anything but a 2 digit number [duplicate]

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 6 years ago.
I have a regex matching a 2 digit number ^(\d{2})$, but need to do the opposite of this. For example, the following need to match:
HK
VK
112
1H
V1
But the following would not match
14
15
91
00
99
Is there any way I can just invert my regex? I know I could check for a match and invert the result, but that is not possible within my constraints.
You can try this:
^([^\d]{2}|\d[^\d]|[^\d]\d|.|.{3}.*)$