Edik Simonian
Edik Simonian

Reputation: 23

Regex expression for finding special numbers

I need help to write a regex expression to catch following cases:

number*number like 1242*1242 or 333*333. Both side of * are the same number but they can have different length.

Upvotes: 0

Views: 76

Answers (2)

Eugene Ryabtsev
Eugene Ryabtsev

Reputation: 2301

This would fix Ofer's answer:

\b(\d+)\*\1\b

The explanation:

\b - word boundary
( - start capturing group
\d+ - digits (one or more)
) - stop capturing group
\* - literal *
\1 - matches exactly what is captured by group 1
\b - word boundary

Upvotes: 4

Ofer Zelig
Ofer Zelig

Reputation: 17480

Here it is:

(\d+)\*\1

It guarantees that the right side is the same as the left side.

EDIT

To ensure no false positives (although I would argue if this should be an issue if programming correctly) use this version (slightly different than Eugene's, a little bit more elegant):

\b(\d+)\*\1\b

Upvotes: 1

Related Questions