I need some help here
Here is example of what im trying to match:
1 ScreenMail Enable friendly none Internal any 5
I need to match everything excluding the last digits (5) Meaning matching the first digit(1), spaces, letter, special characters, etc I tried using /^(\d), but after matching the first digits, it stopped. Your assistance would be appreciated.
The simplest way is probably to remove last digits with:
\d+$
\d+\s*$
See the regex demo.
You may want to use a matching regex like
^.*[^\d\s]
that matches any zero or more chars other than line break chars (.*) as many as possible and then a char other than a digit and whitespace. See this regex demo.
However, if the digits are followed with an optional whitespace, or if you allow any text after the last digits, it will fail. You can then use
^.*[^\d\s](?=\s*\d)
See this regex demo. The (?=\s*\d) positive lookahead requires zero or more whitespaces and then a digit immediately to the right of the current location.
Related
I'm looking for a regex pattern which can do this exactly.
Should match the length which is 12 characters alphaNumeric
Should also check for the occurrence of hyphen - twice in the word
No spaces are allowed.
I have tried the following regex:
^([a-zA-Z0-9]*-[a-zA-Z0-9]*){2}$
Some sample cases
-1234abcd-ab
abcd12-avc-a
-abcd-abcdacb
ac12-acdsde-
The regex should match for all the above.
And should be wrong for the below
-abcd-abcd--a
abcd-abcdefg
I've been using this regex ^([a-zA-Z0-9]*-[a-zA-Z0-9]*){2}$ for matching the above patterns, but the problem is, it doesn't have a length check of 12. I'm not sure how to add that into the above pattern. Help would be appreciated.
Use this:
(?=^.{12}$)(?=^[^-]*-[^-]*-[^-]*$)[a-zA-Z0-9-]+ /gm
The first positive lookahead asserts the total length to be 12.
The second positive lookahead asserts the presence of exactly two hyphens.
Rest is just matching the possible characters in the character set.
Demo
Let's say I've have the string ">=3.0.0". Regex: [^\D](\S+\d+) helps to strip it down to 3.0.0 (https://regex101.com/r/XUxyEM/1). But when I've the string ">=6" Regex [^\D](\S+\d+) will return an empty result (https://regex101.com/r/XUxyEM/2) whilst I want to have 6 as result. How to change my regex so that it works for both cases. Sorry, I'm kind of a regex-newbie.
If you want to capture digit followed with any amount of digit and dots after any amount of non-digits, use
\D*(\d[\d.]*)
See the regex demo
The following regex
\d[\d.]*$
will match a digit followed with 0+ digits and/or dots at the end of the string. See another demo.
I have got the following regex expression so far:
used-cars\/((?:\d+[a-z]|[a-z]+\d)[a-z\d]*)
This is sort of working, I need it to match basically ANYTHING apart from JUST numbers after used-cars/
Match:
used-cars/page-1
used-cars/1eeee
used-cars/page-1?*&_-
Not Match:
used-cars/2
used-cars/400
Can someone give me a hand? Been trying get this working for a while now!
There are few shortcomings of your regex used-cars\/((?:\d+[a-z]|[a-z]+\d)[a-z\d]*).
It's checking for used-cars/ followed by multiple digits then one character within a-z OR multiple characters within a-z then one digit.
[a-z\d]* is searching for either characters or digits which is also optional.
It's inaccurate for your pattern.
Try with following regex.
Regex: ^used-cars\/(?!\d+$)\S*$
Explanation:
used-cars\/ searches for literal used-cars/
(?!\d+$) is negative lookahead for only digits till end. If only digits are present then it won't be a match.
\S* matches zero or more characters other than whitespace.
Regex101 Demo
I need to get a regex that will find a match of a single lower case a-z character followed by 5 numbers that is either:
at the start of a line
at the end of a line
surrounded by () or []
surrounded by whitespace
So the following results are expected:
a12345 MATCH
(a12345) MATCH
[a12345] MATCH
text a12345 MATCH
aa12345 NO MATCH
At the moment I have this (?<=[])]*)[a-z]{1}[0-9]{5}(?=[])]*) but it is not working for all scenarios, for example it sees aa12345 and a12345a as being matches when I don't want them to.
Can anyone help?
EDIT:
Apologies I should have mentioned this is for .NET c#
First of all your should mention programming language.
Following solution is for PCRE.
Regex: ((?<=[\[( ])|^)[a-z]\d{5}((?=[\]\) ])|$)
Explanation:
((?<=[\[( ])|^) checks for preceding brackets, whitespaces OR beginning.
[a-z]\d{5} checks for alphabet followed by 5 digits.
((?=[\]\) ])|$) checks for succeeding brackets, whitespaces OR end of line.
Regex101 Demo
Does this work:
(\[[a-z]\d{5}\])|(\([a-z]\d{5}\))|(\b[a-z]\d{5}\b)
I need to extract the last number that is inside a string. I'm trying to do this with regex and negative lookaheads, but it's not working. This is the regex that I have:
\d+(?!\d+)
And these are some strings, just to give you an idea, and what the regex should match:
ARRAY[123] matches 123
ARRAY[123].ITEM[4] matches 4
B:1000 matches 1000
B:1000.10 matches 10
And so on. The regex matches the numbers, but all of them. I don't get why the negative lookahead is not working. Any one care to explain?
Your regex \d+(?!\d+) says
match any number if it is not immediately followed by a number.
which is incorrect. A number is last if it is not followed (following it anywhere, not just immediately) by any other number.
When translated to regex we have:
(\d+)(?!.*\d)
Rubular Link
I took it this way: you need to make sure the match is close enough to the end of the string; close enough in the sense that only non-digits may intervene. What I suggest is the following:
/(\d+)\D*\z/
\z at the end means that that is the end of the string.
\D* before that means that an arbitrary number of non-digits can intervene between the match and the end of the string.
(\d+) is the matching part. It is in parenthesis so that you can pick it up, as was pointed out by Cameron.
You can use
.*(?:\D|^)(\d+)
to get the last number; this is because the matcher will gobble up all the characters with .*, then backtrack to the first non-digit character or the start of the string, then match the final group of digits.
Your negative lookahead isn't working because on the string "1 3", for example, the 1 is matched by the \d+, then the space matches the negative lookahead (since it's not a sequence of one or more digits). The 3 is never even looked at.
Note that your example regex doesn't have any groups in it, so I'm not sure how you were extracting the number.
I still had issues with managing the capture groups
(for example, if using Inline Modifiers (?imsxXU)).
This worked for my purposes -
.(?:\D|^)\d(\D)