-2

There're regexp for even DIGIT everywhere: Example:

4

But nowhere regexp for even number: Example :

568

1 Answers1

0

Regex is designed to find the pattern in the string, it is not something to find and recognize e.g. (Extract prime number from a string). You should better extract consecutive digits and check for (num % 2 == 0).

But still if you want to stick with regex, the pattern provided in comments above needs a little bit adjustment for you case.

You may try this pattern:

\b(\d+??[02468])\b

Demo

EDIT:

I didn't interacted with grep before, but from this doc i found that grep doesn't support all features that is supported for regex engine by other languages.

So i adjusted the pattern based on allowed input, try this pattern:

^([0-9]+[24680]|[02468])$

Tested it here on following inputs:

(Z + (758+1) + X)
(Z + (757+1) + X)
(Z + (123+1) + X)
757
758
123
144
13
14
2

and result is:

758
144
14
2

Hope it helps now!

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • 2
    it will match `12` out of `123` – Ulugbek Umirov Aug 05 '15 at 12:41
  • So need to much EVEN ) If string consists of 123 - your matcher is much. But need to much 114 where mutch 14 – dmitry golubeu Aug 05 '15 at 12:50
  • @dmitrygolubeu, try now. – NeverHopeless Aug 05 '15 at 13:37
  • tried. its ok by look. Grep processor gonna show me whole line if it contains (\d+??[02468]). But it show me all the lines with odd numbers( – dmitry golubeu Aug 05 '15 at 13:58
  • @dmitrygolubeu, try my edited answer. – NeverHopeless Aug 05 '15 at 15:12
  • Grep can show lines contains some matcher: For example: line "(Z + (758+1) + X)" must have mutcher smth like [0-9]{1,3}[02468]. So if GREP find this matcher for 758 in the line - it returns whole line. So I just need mutcher for 758 – dmitry golubeu Aug 05 '15 at 15:30
  • @dmitrygolubeu ... This answer has more information regarding the question than your initial question. You'll get better responses from the community if you put more detail into your question (like samples of strings that you want / don't want to match, other regex that you've tried but that haven't worked ... etc.). – KornMuffin Aug 05 '15 at 18:58