codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

regex to match at least one symbol and at least 2 numbers


By : , Category : php

This will match your tests.

/^
[0-9]+    # first number
s*       # any whitespace
[+*/-] # operand
s*       # any whitespace
[0-9]+    # second number
$/x
ReLated :

Add s to character class

^[^@s]+$
  • s matches spaces.

  • The negated character class [^ ] ensures anything other than @ or space is matched

Regex Example

[d()+-()]

That should do it.

EDIT: But since some agree the escaping is too much, here ya go:

[d+()-]

Im not sure if you want to match more than one, but if that is the case then remove "^" from the beginning and "$" from the end and also make it globally search like this:

/(([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5])/([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]))|(([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5])-([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]))|(([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]))/g

Hope this helps

It's a bit difficult to review the regex without the sample input but couple of observations:

  • [0-9] can be replaced with d (since, you're already using it at the end)

  • [D] is exactly the same as D. It's a character class itself and unless you have some more characters to include it'ss fine without being enclosed in [].

  • (?:,) should simply be , because you neither want to capture it nor it has any quantifiers.

  • ,[W_] Here it seems you want to use the character class but the would escape the first [. If you actually need a literal there; you need to escape it as \ since it's a special character.

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .