2

I'm having a hard time looking for some info about regexes and I'm still struggling with it.

Let say we have this:

0123 4567 8910

The example has spaces but they can be - or , or + or something which is not a number.

I'm trying to get 10 digits (or 5 or 11), detected individually, ignoring every other character different from a digit.

Using this: [0-9] matches ever digit, I know. If I do something like this [0-9]{10} would never check anything since this pattern is looking for all 10 digits to be together and I need the regex to count those 10 one by one no matter if something different from a digit is in the string...

I was working with this regex: [0-9](?=(?:[^0-9]*[0-9]){4,16}(?![^0-9]*[0-9]))

The thing is this detects everything but the last 4 digits and not the specific 10 I need... Any advice on this would be highly appreciated...

EDIT

I was able to do it... in the other way, I mean, I was able to leave 10 unmarked characters... I need to do the oposite in the way this regex is working:

[0-9](?=(?:[^0-9]*[0-9]){10,}(?![^0-9]*[0-9]))

Here's the example.

This is the goal. With a regex, get 5, 10 or 12 first characters like this (example 12 char):

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • This may help (or perhaps someone else can suggest something better later on): https://stackoverflow.com/questions/277547/regular-expression-to-skip-character-in-capture-group – MDR Mar 18 '20 at 18:49
  • Can you provide more info about the possible inputs, and which parts of each you're trying to capture? – Bryan Downing Mar 18 '20 at 19:55
  • Do you wish to confirm that a string contains exactly a given number of digits? If so do you wish to confirm the string contains only digits and the 4 characters in the string `"-, +"`? Can any of those 4 characters be at the beginning or end of the string? Can those characters be consecutive (e.g., `"-,"` or `"--"`)? Can the string be any length as long as it meet the other requirements? One cannot make a sensible suggestion without knowing the answers to these questions. You need to edit your question and give a precise statement of the problem. – Cary Swoveland Mar 18 '20 at 21:35
  • @CarySwoveland: Thank you for your answer, the string is variable in length but is digit composed. The pattern is a credit card group like, but this could be from 10 to 23 digits in groups or straight, if grouped, could be splitted by spaces, commas, hyphens, etc... – Chuck Amarth Mar 19 '20 at 06:25
  • @BryanDowning Thank you for your interest. Answer could be the comment above. – Chuck Amarth Mar 19 '20 at 06:28
  • @MDR: I did something like I'm looking for, I hope somebody can give some advise on this... Thanks for your answer anyway, I'll take a look again, I hope not wasting somebody else's time... – Chuck Amarth Mar 19 '20 at 06:32

4 Answers4

2

Try this:

/(\d\D*){10}/

(group of a digit followed by zero or more non-digits repeated 10 times)

To get digits only:

let arr = '0123 4567 8910'
          .match(/(\d\D*){10}/)[0]
          .split(/\D+|/)

console.log(arr)
Kosh
  • 16,966
  • 2
  • 19
  • 34
  • Thanks again, it tooks the 10 digits as expected, that's good. Is there some way we can avoid select the space or other kind of non-digit character? – Chuck Amarth Mar 19 '20 at 06:37
  • thanks a lot! I've updated the topic, I would appreciate if you can take a look to that regex and let me know if you think we can invert the results somehow... Thanks again! – Chuck Amarth Mar 19 '20 at 16:46
  • @ChuckAmarth, your question looks like [XY Problem](http://xyproblem.info/). Please let me know what you're REALLY need to do. What is your goal (not approach)? – Kosh Mar 19 '20 at 17:22
  • Hi, Kosh! Thanks for your comments. I've updated the post with images. – Chuck Amarth Mar 19 '20 at 21:01
0

I assume that you want to match n intial digits (if there are more, you want to omit the superflouos digits).

To do it, you need a 2-step approach:

  • drop all non-digit chars,
  • attempt to match n digits in the result.

To express it in JavaScript code, define the following function:

function mtch(str, cnt) {
    return str.replace(/\D+/g, '').match(new RegExp('\\d{' + cnt + '}'));
}

Then when you call it trying to match existing number of digits:

var str = '0123 4567 8910';
res = mtch(str, 8);

you will get 01234567.

But if you attempt to match a non-existing number of digits, e.g.

res = mtch(str, 18);

you will get null.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • Thanks for your response, lets say, we already have some regex interpreter. I'm looking to identify the digits so the interpreter can replace those. – Chuck Amarth Mar 19 '20 at 06:26
0

You could use the regex below to confirm:

  • the string begins and ends with a digit and contains only digits and the four characters in the string ", +-";
  • no two characters in the string ", +-" may appear consecutively; and
  • the string contains a specified number of digits (10 digits at the link).

    ^(?=\d[\d ,+-]\d$)(?!.[ ,+-]{2})(?=(?:\D*\d){10}\D*$)

Javascript demo

For each string that matches this regex simply extract the digits (perhaps by converting all non-digits to empty strings).

The regex performs the following operations:

^                 # match beginning of string
(?=               # begin a positive lookahead
  \d[\d ,+-]*\d   # match 1 digit, 0+ chars in char class, 1 digit
  $               # match end of line
)                 # end positive lookahead
(?!               # begin negative lookahead
  .*              # match 0+ chars
  [ ,+-]{2}       # match 2 chars in the char class
)                 # end negative lookahead
(?=               # begin a positive lookahead
  (?:\D*\d)       # match 0+ non-digits followed by 1 digit
  {10}            # execute non-capture group 10 times
  \D*             # match 0+ non-digits
  $               # match end of line
)                 # end positive lookahead
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
  • This was very helpful, thanks, Cary! I've updated the topic. I've fond a way to do the stuff the opposite way, I mean I was able to leave 10 characters unmarked, the thing is doing that inverse. Check it and let me know if we can addapt this to a check behavior like in the udited regex... – Chuck Amarth Mar 19 '20 at 16:49
0

Another way to get the same results, not using regex:

num_of_digits = 10    
results = [int(x) for x in input_string if x.isdigit()][:num_of_digits]