Jump to content

Regex exactly 15 digits


Go to solution Solved by jguinch,

Recommended Posts

Posted

Hi,

I have a problem (simply for you, but not for me !).

My string is "0821210101083128490000040001041437408000805301531233"

My pattern is d{10} and the result is true with the 10 first digits. I want a false answer !! because there is more than 10 digits !! I don't write d{10,) or d{10,)+ !!

What is the good regex ? ! ? !

Thanks

  • Moderators
Posted

ATR,

 

  Quote

I want a false answer !! because there is more than 10 digits

Why use a RegEx? :huh:

#include <MsgBoxConstants.au3>

$sString = "0821210101083128490000040001041437408000805301531233"

If StringLen($sString) > 10 Then
    MsgBox($MB_SYSTEMMODAL, "Error", "More than 10 digits!")
EndIf
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted
  On 10/14/2014 at 4:00 PM, DarthCookieMonster said:

You can use StringLen($string)  to get the length of the string.

See post #2.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  On 10/14/2014 at 4:27 PM, DarthCookieMonster said:

either one works...though #2 uses Regex

Post #2 is Mebla's, they are using StringLen().

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

My contribution. This also checks if the string is containing digits from 0-9.

#include <MsgBoxConstants.au3>

Local $sString = "0821210101083128490000040001041437408000805301531233"

If StringLen($sString) > 10 Or Not StringIsDigit($sString) Then
    MsgBox($MB_SYSTEMMODAL, "Error", "More than 10 digits or not containing digits!")
EndIf

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

jguinch's solution has the merit of
1/ answering the real initial question which seemingly was "What is the good regex ?"
2/ showing to ATR that he was near the correct regex as he already had the d{10} part

Posted

  On 10/14/2014 at 6:21 PM, mikell said:

jguinch's solution has the merit of

1/ answering the real initial question which seemingly was "What is the good regex ?"

2/ showing to ATR that he was near the correct regex as he already had the d{10} part

Not really, as knowing when to use a regular expression is just as important of what is the correct regular expression to use.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Does this mean that in this case a regex was not an appropriate answer ?

OTOH, we may assume that someone who tries to deal with regex already knows the use of StringLen - and just wants to learn something more

Posted

  On 10/14/2014 at 8:32 PM, mikell said:

Does this mean that in this case a regex was not an appropriate answer ?

I never said that, both have "merit" in my opinion.

  On 10/14/2014 at 8:32 PM, mikell said:

OTOH, we may assume that someone who tries to deal with regex already knows the use of StringLen - and just wants to learn something more

I never persume. If someone is coming from another language where they used regular expressions, then chances are they're not familiar with the language as of yet.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • Solution
Posted

So much posts for a so little issue... :wacko:

The question was about Regexp, so I just answered with a Regexp solution.
Now, Melba, DarthCookieMonster and Guinness answered with a different way, which is the simplest way, the way of learning, the obvious way to progress with a script language.
For these reasons, I think it's a good idea to provide these different solutions, for the asker's interest.
A lot of us like to use Regexp, but it's a good thing to sometime make a "booster shot".

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...