A191919
A191919

Reputation: 3442

Regex parse group of numbers

What i have:

1. 25686-47362-04822-08149-48999-28161-15124-63556

2. 25686-47362-04822-08149-48999-28161-15124-6355654534

3. 54354325686-47362-04822-08149-48999-28161-15124-63556

4. 25686-47362-04822-08149-48999-28161-15124-6355654534fds

5. fdsfds54354325686-47362-04822-08149-48999-28161-15124-63556

6. 25686-47362-04822-08149-48999-28161-15124-63556-63556

What i expect to get

1. 25686-47362-04822-08149-48999-28161-15124-63556

I tried something nearest ([0-9]{5,5}){8}

I trying to avoid 2,3,4,5,6.

Upvotes: 0

Views: 62

Answers (4)

dawg
dawg

Reputation: 103814

You can qualify that line with:

/^((?:\D|^)\d{5}){8}$/m 

Demo

Or

/^((?:-|^)\d{5}){8}$/m 

To be more specific with hyphen delimiters.

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Try this

  string source = @"25686-47362-04822-08149-48999-28161-15124-63556";

  bool result = Regex.IsMatch(source, "^[0-9]{5}(-[0-9]{5}){7}$");

Explanation:

  ^               anchor (beginning of the string)
  [0-9]{5}        5 digits group
  (-[0-9]{5}){7}  7 more groups of 5 digits
  $               anchor (ending of the string)

Upvotes: 5

bkis
bkis

Reputation: 2587

You can use this:

^\d+\.\s(\d{5}-?){8}$

It matches a whole line that matches your criteria: A digit or more, a dot, a whitespace, 8 blocks à 5 digits with hyphens.

Upvotes: 1

Hugo REGIBO
Hugo REGIBO

Reputation: 695

I am not sure there is a way to ask for it to "repeat" the grouping, but i would type it like that:

/^([0-9]{5}\-[0-9]{5}\-[0-9]{5}\-[0-9]{5}\-[0-9]{5}\-[0-9]{5}\-[0-9]{5}\-[0-9]{5})/

Upvotes: 1

Related Questions