I want to match only individual numbers from the following sample input:
[2,4,7,9-11]
Regular expression should match 2,4 & 7, but not 9-11.
Your targets have non hyphens fore and aft:
(?<!-)\b\d+\b(?!-)
See live demo.
For single character matching this might suffice. \b is a word boundary and \d indicates that we're looking for a single digit.
\b\d\b
If you would like to omit single Zeros then you would do something like this with a custom range:
\b[1-9]\b
If you're okay with double-digit numbers and zero, then you would add a plus + (means more than one) to the original:
\b\d+\b
To match any single number from the provided that would not part of a range you would use boundaries and look-arounds:
\b(?<!-)\d(?!-)\b
You can learn more about Regex here.
Related
I'm trying to find a regular expression to find [number2] in [number],[number2][word].
So far I've tried with [,](\d*), but it also gets me the comma.
Demo: https://regexr.com/59eqa
You may use:
(?<=,)(\d*)
Regex Demo
Detail:
(?<=,): positive look behind that doesn't consume character but indicate that the number must have , before it
The previous answers do not handle the case that the second (or two numbers) is matched.
If the second number must be captured, this can be done with
\b\d+,(\d+)[A-Za-z]
where the "number2" is contained in captured group 1.
If you want to get the match only, you could use 2 lookarounds, asserting a comma to the left and a char a-zA-Z to the right.
Use \d+ to match 1 or more digits.
(?<=,)\d+(?=[a-zA-Z])
Regex demo
If there should be a digit before the comma as well:
(?<=\d,)\d+(?=[a-zA-Z])
Regex demo
How do I combine two or more regex expressions such that a match occurs only when both expressions are true.
For instance I want to identify Text containing 6 digits (not beginning with a 5) within word boundaries i.e.
\b[0-46-9]\d{5}\b
but I want to exclude Text containing 000000
^(?!.*000000).*$
abc234576c Match
abc534756c No Match
abc000000c No Match
How do I do this?
Try this regex pattern:
\b(?!.*000000)[^0-9]*[0-46-9]\d{5}[^0-9]*\b
This assumes that you are looking to match a six digit number possibly with non numbers both preceding and proceeding it. It also ensures that the number is not 000000 and the number does not begin with 5.
Demo
Your first regex miss one important point, \b is identifying contrast between a word character (digits included) and a non-word character.
When the whole text is needed, that should work:
[a-zA-Z]*[0-46-9]\d{5}[a-zA-Z]*
Combining it with your proper second expression, you would get:
[A-Za-z]*(?!0{6})[0-46-9]\d{5}[A-Za-z]*
You can view the results here.
I need to find all the words in an inputted text that has (?i:val) in it and are no longer that 5 characters.
So far I got: \b([a-zA-Z]*(?i:val)[a-zA-Z]*){1,4}\b
If we take this sample text to look in: In computer science, a value is an expression which cannot be evaluated any further (a normal form). Val is also a match
I get 3 matches (value, evaluated and Val), however evaluated should not match the pattern, as it is too long. What is the right way to get this straight?
Your pattern does not account for the length of the words matched.
Use word boundaries and a lookahead like this:
(?i)\b(?=\w*val)\w{1,5}\b
See regex demo
The regex matches:
\b - a leading word boundary since the next pattern is \w
(?=\w*val) - a lookahead making sure there is a val substring after zero or more word characters
\w{1,5} - matches 1 to 5 word characters
\b - trailing word boundary that stops words of more than 5 characters long from matching
You may use an ASCII JS version of the regex:
/\b(?=[a-z]*val)[a-z]{1,5}\b/i
It's important to understand why the "evaluated" was matched. Note:
[a-zA-Z]* matches the "e"
(?i:val) matches "val"
[a-zA-Z]* matches "uated"
Actually there's not repetition here! The pattern was matched in only one iteration.
You can achieve what you want using lookarounds, but I think that regex is not the best tool for this task. I highly recommend you using other functions depending on what you have.
I need a regular expression that matches only numbers of length 7 (they can have leading zeros). I used the following super easy regex: \b[0-9]{7}\b. However, this regex also matches numbers in e.g. 5254-6408499 and (0241)4013999 (see https://regex101.com/r/zF5hV7/1).
How can I prevent them from being matched? I only want numbers of length 7 having leading and/or trailing spaces.
Depending on the regular expression flavor, you could create your own boundaries:
(?<=^| )\d{7}(?= |$)
This asserts that either the beginning of the string or a space precedes moving on to matching exactly 7 digits only if the engine asserts that either a space or the end of string follows.
You can use this regex:
(?:^|\s)([0-9]{7})(?:\s|$)
and grab captured group #1
Updated RegEx Demo
I hav a list of strings, such as: Ø20X400
I need to extract the first of the numbers - between Ø and X
I've come so far to match the numbers in general with \d+ - as simple as it is...
But I need an expression to get the first value separated, not both of them...
You can use lookarounds (?<=..) and (?=..):
(?<=Ø)\d+(?=X)
or in Java style:
(?<=Ø)\\d+(?=X)
A second way is to use a capture group:
Ø(\d+)X
or
Ø(\\d+)X
Then you can extract the content of the group.
The regex engines I know parse \n as a newline. \d is used for numbers.
The following regex gives you the first number between a Ø and a X in a capture group:
^.*?Ø(\d+)X.*
Edit live on Debuggex
This Regex will do it for you, (\d+?)X, and here is a Rubular to prove it. See, you want to group digits together, but make it non-greedy, ending the evaluation on X.
Try this one:
\d+(?=\D)
Should find first number wich has some not a number ahead
With normal regular expressions, I would say:
Ø(\d+)X
This finds the Ø character, followed by one or more numbers, followed by an X. Also, the numbers will be stored in the first capture group. Capture groups differ from one regex implementation to another, but this would typically be denoted by \1. Capture group zero, \0, is usually the matched string itself. In this version, \d denotes digits 0-9, but if your regex engine uses \n for that purpose, use:
Ø(\n+)X