0

I need help sorting out a solution for a pattern generator.

I have an application that uses exchange formats with different patterns like

00350-ABA-0NZ0:AXYA-11/11/2012 etc.,

that have numeric and alphanumeric data separated by '-','.',":" and '/'. Now what I want to do is convert this to a generic format like nnnnn-ccc-nccn:cccc-nn/nn/nnnn where n is a digit and c is a character.

Any help/suggestions/ideas . . . Thanks CSK.

maaartinus
  • 44,714
  • 32
  • 161
  • 320
srcKode
  • 53
  • 1
  • 1
  • 8
  • I could do it in two steps .... 1.digits string pattern = @"[0-9]"; string new= Regex.Replace(str,pattern, "n"); 2.characters string new= Regex.Replace(str, @"[a-zA-Z]", "c") – srcKode Oct 06 '12 at 20:12
  • It is unclear what you're trying to accomplish. Your example pattern matches your generic format, so it is not readily apparent what the problem is. – lanzz Oct 06 '12 at 20:14
  • i just posted a comment with my solution.The idea was to replace any occurrence of a digit with 'n' and any occurrence of a character with 'c' using a single pattern. – srcKode Oct 06 '12 at 20:18
  • @srcKode: I removed a tag, hope you don't mind. – maaartinus Oct 07 '12 at 18:05
  • I wanted to do it myself;thanks @maaartinus – srcKode Oct 10 '12 at 09:21

1 Answers1

0

You can't do conditional replaces in a single regex. You need to do it in two steps (here's a Python example):

>>> s = "00350-ABA-0NZ0:AXYA-11/11/2012"
>>> s = re.sub(r"[A-Za-z]", "c", s)
>>> s
'00350-ccc-0cc0:cccc-11/11/2012'
>>> s = re.sub(r"\d", "n", s)
>>> s
'nnnnn-ccc-nccn:cccc-nn/nn/nnnn'

And you need to do it this way around - I just saw your solution in your comment, and if you look at it again, you'll see it won't work. Hint: You'll get 'ccccc-ccc-cccc:cccc-cc/cc/cccc' as a result...

Another solution would be to use a callback function that examines the match and chooses the replacement string accordingly. But that's not pure regex anymore:

>>> def replace(m):
...     return "n" if m.group(0).isdigit() else "c"
...
>>> s = re.sub(r"[A-Za-z0-9]", replace, s)
>>> s
'nnnnn-ccc-nccn:cccc-nn/nn/nnnn'
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Thanks Tim ; I actually did the same thing only that I did not use correct variable names in my comment. – srcKode Oct 06 '12 at 20:45
  • @srcKode: I meant that if you do the numbers replacement first, then you'll replace all the `n`s you've just created in your string with `c`s when you do the characters replacement after that... – Tim Pietzcker Oct 06 '12 at 20:49
  • You are right. I did the characters first and then digits. Thanks, I didn't realize it would happen that way had I used it in the reverse way. – srcKode Oct 06 '12 at 20:54
  • Can we know if a string value is a sub-string of a pattern.For e.g if i have a pattern defined like (\d{4})-(\d{2})\/(\d{2}) and i have a string 44/78 can i match this with the pattern? – srcKode Oct 06 '12 at 21:35