Question: i want to replace string from a file.
i have a line like this:

```
qwe{data:"dede-dddd-ssss",login:"user",display:"screen"},abc,xyz

```

i want to replace all string starting from data to login with spaces

i try this i can only replace the string, is there a way to replace with a starting and endpoint for string?

```
def source = new File("c:\\sq\\file1.txt") 
def dest = new File("c:\\sq\\file2.txt") 

def fileText = source.text
def filedest = dest.text
for ( index in 1..9 ) {
    fileText = (fileText =~ /data/).replaceFirst("")

```

Answer: 
This code has an anchored version (to `{`) of the regex:

```
def fileText = '''\
qwe{data:"dede-dddd-ssss",login:"user",display:"screen1"},abc,xyz
...
qwe{data:"data2",login:"user2",display:"screen2"},abc,xyz
...
qwe{data:"data3",login:"user3",display:"screen3"},abc,xyz\
'''

fileText = fileText.replaceAll(/(?<=\{)\s*data:[^,]+,\s*login:[^,]+,/ , '')
println fileText

```

Output:

```
qwe{display:"screen1"},abc,xyz
...
qwe{display:"screen2"},abc,xyz
...
qwe{display:"screen3"},abc,xyz

```

Try also in the [online groovy web console](https://groovyconsole.appspot.com/script/5177290749116416)

**UPDATE** 

If a understand correctly your request: this following code read the input text from `source` (`c:\\sq\\file1.txt`) and put the modified text into destination file `dest` (`c:\\sq\\file2.txt`):

```
def source = new File("c:\\sq\\file1.txt") 
def dest = new File("c:\\sq\\file2.txt") 

dest.write(source.text
  .replaceAll(/(?<=\{)\s*data:[^,]+,\s*login:[^,]+,/ , '') )

```
If you want it in the files, then you can try below, and find the updated data in test2.txt:

note:please update the file paths as per your environment

Courtesy @GsusRecovery for regex

```
def fileContent = new File('/tmp/test.txt').text
def replacedContent = fileContent.replaceAll(/(?<=\{)\s*data:[^,]+,\s*login:[^,]+,/ , '')
def newFile = new File('/tmp/test2.txt')
newFile.write(replacedContent)

```