0

I'm using C# to create a pattern in order to replace all the occurrences in a string from

RX123456789 into RX*********

I have tried various patterns without success. I'm kinda new regarding Regular Expression.

I appreciate your help.

redviper2100
  • 265
  • 4
  • 15
  • 1
    Please show what patterns you *have* tried - and any other constraints. For example, are you happy just to replace *all* digits in the string with *, or do you need to make sure they're only after "RX"? – Jon Skeet Sep 27 '18 at 11:00
  • Is the count of '*' always fixed? – Samvel Petrosov Sep 27 '18 at 11:00
  • Is the requirement just to replace all numbers in a string with a *? Or is the RX123456789 going to appear as part of a larger string which may have other numbers and you just need to match numbers starting RX? Also is the length of the string of numbers fixed or variable? – Chris Sep 27 '18 at 11:01
  • Yes, I want to make sure they are after RX – redviper2100 Sep 27 '18 at 11:02
  • show some of the patterns you have tried. And show some more context of the string, ie, wheter each of this expressions is in a separate line, separators, ... I suppose the number of * should match the number of digits replaced? – derpirscher Sep 27 '18 at 11:03
  • You only have numbers after RX? you want to replace everything after RX until you have a whispace? You should put a full string of a possible input you may have and then show us the expected output. And remember, when trying regex: https://regex101.com/ – Mikitori Sep 27 '18 at 11:04
  • 1
    Try regex `^RX\S*$` – Nambi_0915 Sep 27 '18 at 11:04
  • [This answer](https://stackoverflow.com/a/31006413/3832970) will give you a hint on what regex you can use. – Wiktor Stribiżew Sep 27 '18 at 11:06
  • https://stackoverflow.com/questions/8586954/replacing-digits-in-c-sharp-string this answer will also help – Nithin Mohan Sep 27 '18 at 11:07
  • please check my answer @redviper2100 – Hitesh Anshani Sep 27 '18 at 11:12

1 Answers1

0

Use this Regex:-

string data = "RX123456789";


var resultString = Regex.Replace(data, @"[0-9]", "*");

If you need * of number only When RX is present then use this logic:-

string data = "RX123456789";
var resultString="";
if (new Regex("RX([0-9]+)").IsMatch(data))
{
    resultString = Regex.Replace(data, @"[0-9]", "*");
}
Hitesh Anshani
  • 1,499
  • 9
  • 19
  • If there are other numbers (not prefixed with "RX") AND the requirement is to only replace the prefixed numbers, this will fail (it replaces *any* digit). Granted, the requirements are not entirely clear – Hans Kesting Sep 27 '18 at 11:34
  • if they need only the number to replace with the star where RX is present then they need to use this kind of regex (RX[0-9]+) @HansKesting – Hitesh Anshani Sep 27 '18 at 11:44
  • @D-johnAnshani: You fixed the search part, but not the replace part. Consider a string like `RX1234 some text 5678` this will end up as `RX**** some text ****` which does not seem to meet redviper's requirements – derpirscher Sep 29 '18 at 05:44