I have an value that is not being read by our OCR program correctly. It's predicable so I would like to use a find/replace in regex (because this is how we are already extracting the data).
We get the named group like this: (?<Foo>.*?)
I would like to replace 'N1123456' with 'NY123456'. We know that we expect NY when we are getting N1.
What can I try to get this done in the same regular expression?
Edit: (?<Foo>.*?)
Make groups of non digits and digits and add Y
after non digit group.
(\D+)(\d+)
Here is demo
Enclose it inside \\b
or ^
and $
for better precision.
Sample code:
PHP:
$re = ""(\\D+)(\\d+)"";
$str = "N1123456";
$subst = '$1Y$2';
$result = preg_replace($re, $subst, $str, 1);
Python:
import re
p = re.compile(ur'(\D+)(\d+)')
test_str = u"N1123456"
subst = u"$1Y$2"
result = re.sub(p, subst, test_str)
Java:
System.out.println("N1123456".replaceAll("(\\D+)(\\d+)","$1Y$2"));
If you expect N1
to be always followed by 6 digits, then you can do this:
Replace this: \\bN1(\\d{6})\\b
with this: NY$1
.
This will replace any N1
followed by 6 digits with NY
.
This is what I would do:
Dim str = Regex.Replace("N1123456", @"\bN1(\d+)", "NY$1");
The Expression to find the text is N1 followed by numbers like : \\bN1(\\d+)
.
The numbers belongs to the group(1) I would like to preserve and attach to NY during replacing: NY$1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.