Use regular expression to replace character in specific position

I want to replace the dash- to space from a string at position 12.
I cannot use replace("-"," ") because there may some negative number in the string.

 K120122222-01       1100625     TL             -11,008                 0           -11,008
 I200522222-01       1101029     PDDW              -311                 0              -311
 A122222221-03       1101101     PWL                530                 0               530
 A1271121212-03       1101101     PWL                458                 0               458                                                                                      

I want the result like below.

 K120122222 01       1100625     TL             -11,008                 0           -11,008
 I200522222 01       1101029     PDDW              -311                 0              -311
 A122222221 03       1101101     PWL                530                 0               530
 A1271121212 03       1101101     PWL                458                 0               458                                                                                      

I had try this but it doesn’t work.
System.Text.RegularExpressions.Regex.Replace(strTxt, “/(?<=^(?:.{11}))./gm”," ")
Are there any solution?


Clarification needed you need to replace the one column - with Space?

If yes then your requirement would be follows as below.

Read range the Excel

For each row in readDt

Assign Currentrow(“YourTargetColumn) =Currentrow(“YourTargetColumn”).ToString.Replace(”-"," ")

Then Outside the loop Use write range as same readrange path and Dt


1 Like

All the data are read from a txt file.
I need to separate them in 7 columns by Generate DataTable.
So I need to replace - into space.


Try with the pattern below!


why it work fine in Regex test website,but not in uipath?
I tried this:

System.Text.RegularExpressions.Regex.Replace(strTxt, "/(^[^-]*)-(.*)/gm"," ")

It’s also not work in matches.

I tried this. So I need to separate in newline?


Another Alternate way of doing the above !

Please follow the steps below!

Use generate datatable to Create a datatable

Then Use The function to add new column


Use For each row to iterate the datatable

Split the first Column based on the “-”

then use assign to add the new value to the column!

Assingn CurrenRow(“NewColumn”)=CurrentRow(“firstColumn”).toString.split("-"c)(1)

Then Out side the loop use write range the values!


You can adapt the quantifiers as per your business needs for example if you have more than 2 digits in the end then you can change the regex quantifier as needed.

The following extracts the hypens alone and not the negative symbols before digits.
Link : Your test string in regex101 Kindly read through the Explanation on how to use Positive lookahead and lookbehind on the right side of this link.


In short, when this pattern digit-digit is found please replace the - with a SPACE. As below.

System.Text.RegularExpressions.Regex.Replace(strTxt, “(?<=\d)-(?=\d{2,4})”," ")

In the end, you should have 7 columns with space as seperator.

1 Like
Have a look on following approach

(?<=[A-Z,\d]{10})(\-)(?=\d+\b)|(\ ){2,}

with replacing to the pipe (|) we cleansed up the data and can pass it later to generate datatable activity for getting parsed the texted into a datatable

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.