Results 1 to 22 of 22

Thread: Find/Replace a 'block' of text in a text file?

Threaded View

  1. #18
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Find/Replace a 'block' of text in a text file?

    Quote Originally Posted by relentlesstech View Post
    The file is slightly larger - It is a CFG file that holds all the data to 'create' a vehicle in one of my games ...
    That will make things a bit easier then if we can use the System.IO.File methods.

    I came up with this:
    vbnet Code:
    1. Dim fileText As String = File.ReadAllText("F:\file.txt")
    2. Dim attchRegion As String = String.Join(Environment.NewLine, _
    3.                             Regex.Matches(fileText, "attach\t\t0x\d{8}\b 1 ; \d{2}\b") _
    4.                             .Cast(Of Match)().Select(Function(m) m.Value))
    5.  
    6. fileText = fileText.Replace(attchRegion, "NEW REPLACEMENT VALUE")
    7. TextBox1.Text = fileText

    And from the input file I had:
    # Vehicle importer wizard generated vehicle configuration file #

    center -0.005 0.670 0.019

    material 0x00000000 0x0005000B 0x0005000B

    compound
    compound
    chassis
    body 0.0 0.0 0.0 0.0 0.0 0.0 0.001 box 0.001 0.001 0.001
    noclick
    bone
    body 0.001 -0.227 -1.833 0.000 0.000 0.000 10.000 cars\racers\Axis_V3\meshes\_phys_chassis_F.scx ;0x0000000C

    # FL wheel #
    type 10.000 sphere 0.44
    spring 0.290 0.100 0.050
    wheel -0.785 -0.544 -1.305 0.000 0.000 0.000 1.000 1.000 1.000 0.000
    wheelbones
    slot 0.000 0.000 0.000 0.000 0.000 0.000 101 ; FL wheel
    attach 0x00040100 1 ; 14
    attach 0x00040101 1 ; 15
    attach 0x00040102 1 ; 16
    attach 0x00040103 1 ; 17
    attach 0x00040104 1 ; 18
    attach 0x00040105 1 ; 19
    attach 0x00040106 1 ; 20
    attach 0x00040107 1 ; 21

    slottype 101
    slotdmgmode 0x00000005

    slot 0.000 0.000 0.000 0.000 0.000 0.000 201 ; FL suspension arm
    slottype 301
    slotdmgmode 0x00000006
    It gave me:
    # Vehicle importer wizard generated vehicle configuration file #

    center -0.005 0.670 0.019

    material 0x00000000 0x0005000B 0x0005000B

    compound
    compound
    chassis
    body 0.0 0.0 0.0 0.0 0.0 0.0 0.001 box 0.001 0.001 0.001
    noclick
    bone
    body 0.001 -0.227 -1.833 0.000 0.000 0.000 10.000 cars\racers\Axis_V3\meshes\_phys_chassis_F.scx ;0x0000000C

    # FL wheel #
    type 10.000 sphere 0.44
    spring 0.290 0.100 0.050
    wheel -0.785 -0.544 -1.305 0.000 0.000 0.000 1.000 1.000 1.000 0.000
    wheelbones
    slot 0.000 0.000 0.000 0.000 0.000 0.000 101 ; FL wheel
    NEW REPLACEMENT VALUE
    slottype 101
    slotdmgmode 0x00000005

    slot 0.000 0.000 0.000 0.000 0.000 0.000 201 ; FL suspension arm
    slottype 301
    slotdmgmode 0x00000006
    I'm assuming the replacement value will be another bunch of "attach 0x00040104 1 ; 18" similar values. So as long as you String.Join() these values with a newline, then when it comes time to use my Regex LINQ, then it will match every one of those lines in the textfile of those attach lines.

    Note: For my regex, I assumed that the 1 in each of those lines will never be a different number, if it is, I can just change my regex around a bit.

    If it's only going to always be one digit:
    Code:
    attach\t\t0x\d{8}\b [0-9] ; \d{2}\b
    Last edited by AceInfinity; Aug 19th, 2012 at 03:14 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width