I want to replace the dash(-) to space from a string at position 12. I cannot use replace("-"," ") because there may a negative number in the string.
I had try (?<=^(?:.{11})). but it only get the result at first line.
Are there any solution?
K120613763-01 1100625 TL -11,008 0 -11,008
N123203270-02 1101024 PWL -387 0 -387
I200543078-01 1101029 PDDW -311 0 -311
A122285277-03 1101101 PWL 530 0 530
A127477426-03 1101101 PWL 458 0 458
I want the result like below.
K120613763 01 1100625 TL -11,008 0 -11,008
N123203270 02 1101024 PWL -387 0 -387
I200543078 01 1101029 PDDW -311 0 -311
A122285277 03 1101101 PWL 530 0 530
A127477426 03 1101101 PWL 458 0 458
Note: Lines start with a space
I use vb.net to programming in uipath.
I tried this but it doesn't work:
System.Text.RegularExpressions.Regex.Replace(strTxt, "/(?<=^(?:.{11}))./gm"," ")
CodePudding user response:
You can match a hyphen after 11 characters and use a multiline notation like
System.Text.RegularExpressions.Regex.Replace(strTxt, "(?m)(?<=^.{11})-"," ")
CodePudding user response:
It's all flavor dependent, but usally ^
will me mean start of input, Use the m
ultiline modier /re/m
:
/(?<=^(?:.{11}))./m
and a replace function sometimes only replaces the first occasion of the pattern use the g
lobal modifer as well: /re/g
:
/(?<=^(?:.{11}))./gm
CodePudding user response:
Use a negative look behind to match the 12th character:
(?<=^.{11}).
(see live demo)
And replace it with a -
You'll need to use the "global" option when doing the replace.
CodePudding user response:
/(?<=^\s\S{10})-(?=.*$)/gmi
Explanation
- Positive Lookbehind
(?<=^\s\S{10})
Assert that the Regex below matches:^
asserts position at start of a line\s
matches any whitespace character (equivalent to[\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]
)\S
matches any non-whitespace character (equivalent to[^\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]
){10}
matches the previous token exactly 10 time
-
matches the character-
with index 4510 (2D16 or 558) literally (case insensitive)- Positive Lookahead
(?=.*$)
Assert that the Regex below matches:.
matches any character (except for line terminators)*
matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)$
asserts position at the end of a line
- Global pattern flags
g
modifier: global. All matches (don't return after first match)m
modifier: multi line. Causes^
and$
to match the begin/end of each line (not only begin/end of string)i
modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
A working example can be found here: https://dotnetfiddle.net/cLZeka
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?<=^\s\S{10})-(?=.*$)";
string substitution = @" ";
string input = @" K120613763-01 1100625 TL -11,008 0 -11,008
N123203270-02 1101024 PWL -387 0 -387
I200543078-01 1101029 PDDW -311 0 -311
A122285277-03 1101101 PWL 530 0 530
A127477426-03 1101101 PWL 458 0 458 ";
RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}