Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change String - Add and Replace Characters 2

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
0
0
US
Hi, I am hoping the experts can point me in the right direction on this one. I have never attempted this and do not know where to begin. Is this a regular expression function? I don't know.... so here is what I need to do:

Change this:
Code:
 CR4-7,CR10,CR40-42

To This:
Code:
 CR4,CR5,CR6,CR7,CR10,CR40,CR41,CR42

How to add the missing numbers where there is a "-" along with adding the "CR" to the prefix. If someone can lead me a little that would be great.
 
Of course regular expressions are great tool to test if the element is a compound element i.e. CR<from_num>-<to_num>

For example you can do something like this:
Code:
my_line_in [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"CR4-7,CR10,CR40-42"[/color]
my_line_out [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]""[/color]
[COLOR=#0000ff]' create regexp object[/color]
[COLOR=#804040][b]set[/b][/color] re [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]new[/b][/color] regexp
[COLOR=#0000ff]' regexp pattern[/color]
re[COLOR=#804040][b].[/b][/color]pattern [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"CR\d+\-\d+"[/color]
[COLOR=#0000ff]' split string into array[/color]
my_array [COLOR=#804040][b]=[/b][/color] [COLOR=#a020f0]split[/color][COLOR=#804040][b]([/b][/color]my_line_in[COLOR=#804040][b],[/b][/color][COLOR=#ff00ff]","[/color][COLOR=#804040][b])[/b][/color]

[COLOR=#0000ff]' process array elements[/color]
[COLOR=#804040][b]for[/b][/color] [COLOR=#804040][b]each[/b][/color] array_element [COLOR=#804040][b]in[/b][/color] my_array
  [COLOR=#804040][b]if[/b][/color] re[COLOR=#804040][b].[/b][/color]test[COLOR=#804040][b]([/b][/color]array_element[COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]then[/b][/color]
   [COLOR=#0000ff] ' if the element match the pattern, i.e. compound element[/color]
    my_string [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]replace[/color][COLOR=#804040][b]([/b][/color]array_element[COLOR=#804040][b],[/b][/color][COLOR=#ff00ff]"CR"[/color][COLOR=#804040][b],[/b][/color][COLOR=#ff00ff]""[/color][COLOR=#804040][b])[/b][/color]
    my_range[COLOR=#804040][b]=[/b][/color][COLOR=#a020f0]split[/color][COLOR=#804040][b]([/b][/color]my_string[COLOR=#804040][b],[/b][/color][COLOR=#ff00ff]"-"[/color][COLOR=#804040][b])[/b][/color]
    from_num [COLOR=#804040][b]=[/b][/color] my_range[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]0[/color][COLOR=#804040][b])[/b][/color]
    to_num[COLOR=#804040][b]=[/b][/color]my_range[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]1[/color][COLOR=#804040][b])[/b][/color]
    [COLOR=#804040][b]for[/b][/color] i[COLOR=#804040][b]=[/b][/color]from_num [COLOR=#804040][b]to[/b][/color] to_num
      my_element [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"CR"[/color] [COLOR=#804040][b]&[/b][/color] i
      [COLOR=#804040][b]call[/b][/color] add_to_line_out
    [COLOR=#804040][b]next[/b][/color]
  [COLOR=#804040][b]else[/b][/color]
   [COLOR=#0000ff] ' if it's simple element[/color]
    my_element [COLOR=#804040][b]=[/b][/color] array_element
    [COLOR=#804040][b]call[/b][/color] add_to_line_out
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
[COLOR=#804040][b]next[/b][/color]

[COLOR=#0000ff]' write input[/color]
WScript[COLOR=#804040][b].[/b][/color]StdOut[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]writeline[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"my_line_in  = "[/color] [COLOR=#804040][b]&[/b][/color] my_line_in[COLOR=#804040][b])[/b][/color]
[COLOR=#0000ff]' write output[/color]
WScript[COLOR=#804040][b].[/b][/color]StdOut[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]writeline[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"my_line_out = "[/color] [COLOR=#804040][b]&[/b][/color] my_line_out[COLOR=#804040][b])[/b][/color]

[COLOR=#804040][b]sub[/b][/color] add_to_line_out
 [COLOR=#0000ff] ' add my_element to string[/color]
  [COLOR=#804040][b]if[/b][/color] my_line_out [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]""[/color] [COLOR=#804040][b]then[/b][/color]
    my_line_out [COLOR=#804040][b]=[/b][/color] my_element
  [COLOR=#804040][b]else[/b][/color] 
    my_line_out [COLOR=#804040][b]=[/b][/color] my_line_out [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]","[/color] [COLOR=#804040][b]&[/b][/color] my_element
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
[COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]sub[/b][/color]

Output:
Code:
C:\Work>cscript /NoLogo nu2java2.vbs
my_line_in  = CR4-7,CR10,CR40-42
my_line_out = CR4,CR5,CR6,CR7,CR10,CR40,CR41,CR42
 
And a non-regex solution. Same idea though:

Code:
sStart = "CR4-7,CR10,CR40-42"

arr1 = Split(sStart, ",")
For x = LBound(arr1) To UBound(arr1)

   If InStr(arr1(x), "-") > 0 Then
      sExpand = ""
      arr1(x) = Replace(arr1(x), "CR", "")
      arr2 = Split(arr1(x), "-")

      For i = CInt(arr2(0)) to CInt(arr2(1))
         sExpand = sExpand & ",CR" & i
      Next
      sExpand = Mid(sExpand, 2) 

      arr1(x) = sExpand
   End If
Next

sEnd = Join(arr1, ",")
 
Hi guitarzan,
But, by the way, it looks like this:
if you do not get correct data in your script (what could happen), for example instead of
sStart = "CR4-7,CR10,CR40-42"
you will get
sStart = "CRAB-7,CR10,CRXY-42"
then it seems that your script crashes on the conversion using CInt().

 
Correct, what I posted is not production-ready code, it's just an illustration.
 
Thanks mikrom & guitarzan... this is more than enough to get me started. I appreciate the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top