Link to home
Start Free TrialLog in
Avatar of Berhan Karagoez
Berhan KaragoezFlag for Sweden

asked on

RegEx suggestion?

REGEX help please:

I'd like the following rules implemented as regex:

1) max chars in string : 7 - 8  - something like {7,8}
2) starting chars have to be  one of  the following (2,4,12)
3) upon typing 2345 I want the replacement be 2000345 , that is 7 chars starting with first char and ending with last 3 and zerofill in-between

Is that possible?
Avatar of David Favor
David Favor
Flag of United States of America image

You basic approach will be something like this...

if ($str =~ /(\d)(\d+)/o) {
   $str = $1 . "000" . $2;
}

Open in new window


Then change your 2x accumulated expressions ($1 + $2) to whatever pattern you like.

I couldn't quite follow exactly what your asking.

Maybe provide a collection of starting data + ending data. Likely someone can help.
Note: Anything's possible with PERL... Well... any string processing...
I'm not quite following what you are asking either.

Are you saying that this is checking input?  Someone will provide a number and:
  • it must be 8 characters max
  • the starting digits must be 2, 4, or 12
  • if it is less than 7 characters, then take the starting 2, 4, or 12 and separate it from the "rest" with 0s

If so, this should do what you want:
$input = '2345';  # put into the var in whatever way you want
if ($input =~ s{^(?:(2|4)(\d{0,7})|(12)(\d{0,6}))$}{($1?$1:$3).('0' x (7-length($input))).($2?$2:$4)}e) {
    print $input, "\n";
} else {
    die "$input is invalid";
}

Open in new window

Avatar of Berhan Karagoez

ASKER

David, thank you.

This was not supposed to be a perl specific regex, but just a regex. Does it make any sense?
Is my understanding that this is about input (and the bullet points) correct?

What exactly are you looking for?  A single regex to do everything is impossible (unless I've misunderstood what you want).  Again, assuming I've understood what you want, it is possible with a regex that supports Perl extensions (I'm not sure if anything besides Perl supports executable code on the replace side - if it does, my solution should still work but you have to replace the Perl code syntax with something else).
wilcoxon, thank you.

^(?:(2|4)(\d{0,7})|(12)(\d{0,6}))$ is awesome.
This is for validation. PERFECT!

I was thinking one can do the replacement with regex only as well, maybe this is not possible.

So to clarify, the validation is exactly on point.

The idea is to replace ( if possible at all ) inputs to have a certain format, for example:

2345     would become 2000345 ( 7 characters )
45678   would become 4005678 (7 characters too)

12345 would become 12000345 (8 characters )
123456 would become 12003456 (8 characters too)
1234567 would become 12034567 (8 characters too)

SO it is  two parts of the string one can say [ A ][ B ], where [ A ] = 2 or 4 or 12 and [ B ] is ALWAYS 6 chars long zero-filled string from left to right.

Again I am not sure if this is possible without scripting but just with regex.

I am thinking RegEx as two steps,
step 1 is pattern validation and
step 2 is replacement
and exploring the possibility if RegEx can be used in both steps, but I am not sure second step (replacement) is possible with only regex (without scripting).
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Which language or tool are you using? Replacements can be done with lots of different regex tools, but not all of them.
Hi Terry, thanks.

I am using http://regexstorm.net/tester
Guys thank you,
I got some very good answers. I think this is not possible entirely with RegEx alone because using two char as one of the options, had it been only check for the first chars only, ie 2,4,6 and not 2,4,12 then it would perhaps been easier to do this without scripting. Whichever, I am happy with what I got from this, we can close this.

Thank you.
Thanks guys.

I felt [ wilcoxon ] delivered the solution.I want to move on, not to spend too much time on this.