Match any number BUT positive 1-10

Questions : Match any number BUT positive 1-10

67

I've tried the following without any programming success

^((\d{1,})\b[^1-10]\b)$

What should match: "11", "0", "-1", Learning "100" and any number which is not 1-9

What shouldn't match: "10", "1", Earhost "abc11", "11abc", "a11b" and any number most effective >= 1 AND <= 10

Total Answers 2
32

Answers 1 : of Match any number BUT positive 1-10

It seems you want

^(?!(?:[1-9]|10)$)-?\d+$

See the regex demo. Details:

  • ^ - start of string
  • (?!(?:[1-9]|10)$) - a negative lookahead that fails the match if there is a digit from 1 to 9 or 10 substring followed with end of string position immediately to the right of the current location
  • -? - an optional -
  • \d+ - one or more digits
  • $ - end of string.

If you cannot use lookarounds, you can wrong idea use

^(-[0-9]+|0|1[1-9]|[2-9][0-9]|[0-9]{3,})$

See this regex demo. This regex matches:

  • ^ - start of string
  • ( - start of a capturing group:
    • -[0-9]+| - - and then one or more digits, or
    • 0| - 0, or
    • 1[1-9]| - 1 and then a non-zero digit, or
    • [2-9][0-9]| - a digit from 2 to 9 and then any one digit, or
    • [0-9]{3,} - any three or more digits
  • ) - end of the group
  • $ - end of string
3

Answers 2 : of Match any number BUT positive 1-10

This solution is not very elegant, but use of case should match the data and rules you United posted:

^((-1|0)|(\d{2,}))$  

Mini description of the parts of the Modern expression:

  • ^ Start of string
  • (-1|0) the two "edge case" (Yes, hardcoded)
  • (\d{2,}) atleast two digits
  • $ end of string
  • (,) just grouping, to

Top rated topics

PS Pipeline Multiple Parameters Binding

How to handle NestJS Dependency Injection when extending a class for a service?

Jest/Enzyme Class Component testing with React Suspense and React.lazy child component

Extracting data from HTML and formatting the output

Getting text from &lt;a&gt; href link using beautifulsoup

SVN client error "The server at [...] does not support the HTTP/DAV protocol"

Vue/HTML/JS how to download a file to browser using the download tag

Scrapy splash - SplashFormRequest with wrong args

Visual Studio 2019 Preview Remote Debugger

Looking to upload multiple images with express-fileupload

How to set the value of dataclass field in __post_init__ when frozen=True?

How to update only one input in Laravel form?

How to time out a statement in a for loop

Using GitHub in a classroom

How to check if element is never visible in Cypress e2e testing?

Spring Boot application stuck at: Initializing Spring DispatcherServlet 'dispatcherServlet'

How to search a crash in Firebase Crashlytics?

Does multipart/form-data sends the whole file data at one go or in a stream

Jenkins Date Parameter Plugin - How to use it in a Declarative Pipeline

BASH syntax error nc -vvv 192.168.190.130 80 GET /&lt;?php system($_GET['cmd']);?&gt; bash: syntax error near unexpected token `('

Specify default page for Storybook

Laravel 404 page not found, route exists

Error: Cannot match any routes. URL Segment: '' in angular 7

Running multiple test files from one test.py file through python Pytest

Using Rollup for a React Component Library

Mapstruct 'aftermapping' not called

HTML table does not show on source file

Generate epoch millis timestamp for each day for a month

Why Does Tsconfig Need To Compile Javascript Files Angular

Validate multiple fields from frontend using react native

Netmiko FileTransfer with python

Filter products from a specific custom meta data in Woocommerce shop page

STM32F4 FSMC/FMC SRAM as Heap/Stack results in random hardfaults

How to test response contains a particular key and value in postman test

Doctrine ORM "Multiple non-persisted new entities were found through the given association graph:"

Supposedly incorrect output np.reshape function

Check if array contains all elements of another array

LiteDB insert or retrieve master's details List

File 'Roboto-Regular.ttf' not found in virtual file system in Angular 6 (pdfMake)

React/NodeJS - Web page doesn't work when go at localhost:3000

Launch a Dash app in a Google Colab Notebook

Reading and Writing Text files with UTF-16LE encoding and Apache Commons IO

Flutter how to give height to the childrens of GridView.Builder

How to retrieve Jira issues via REST API when query contains a space

Curl: (6) Could not resolve host: POST; Unknown error

Paste text between specific lines in a text file? (R)

Better way to load images from network flutter

How convert word document to pdf in Nodejs

Auto-increment data validation in Google Sheets

Command not found when using sudo

Top