-1

I have a string where i want to replace some chars by empty chars inside a string.

For eg my string is :

"<anyKey>2018-10-08T00.00000-07:00</anyKey>"

The regex i use is :

return System.Text.RegularExpressions.Regex.Replace(sb.ToString(), "<{1}[0-9a-zA-Z]*Key>{1}[0-9]{4}-[0-9]{2}-[0-9]{2}(?<timeandzone>T[0-9]{2}.[0-9]{5}-[0-9]{2}:[0-9]{2})</{1}[0-9a-zA-Z]*Key>{1}", ""); 

So the output i get is empty. It means that my regex is correct and it is replacing it with empty string, but i just want output like :

"<anyKey>2018-10-08</anyKey>"

I just want to find something that starts with "T" inside the tag where "Key" word is there and replace it with "".

Can you tell me what should i change here ?

Update : I have an XML which is 1000 lines, but it is stored in a String, so in some cases i need to replace this thing.

s shah
  • 21
  • 1
  • 6
  • Its an infamous post on SO but it doesn't apply here. He isn't parsing an HTML/XML document. This isn't even parsing in the strictest sense of the word. This is the pattern matching that REGEX exists to solve. – DetectivePikachu Oct 16 '19 at 20:16
  • 1
    If you want to restrict your replacement to text that is found within an HTML or XML element, then you are absolutely trying to parse the data. The marked duplicate provides details of why that's hopeless, as well as things you can try if you insist. If you get to the point where you've isolated the text you actually want to modify (i.e. by using an actual parser and extracting the node of interest) and still are having trouble replacing content, post a new question in which you provide a good [mcve] showing what you've tried, and explaining why you can't figure it out. – Peter Duniho Oct 16 '19 at 20:21
  • We couldn't assume he was parsing entire documents based off one line that is valid XML. Its easy to be snarky when you have the benefit of an edited answer that adds additional detail, but it isnt necessary. – DetectivePikachu Oct 16 '19 at 20:26
  • Yes, sorry for not making it clear. I have a huge text and in some case (if a flag is true), then only i need to remove everything inside a tag which has "Key" word and remove everything from "T". – s shah Oct 16 '19 at 20:29

1 Answers1

1

I usually bury RegEx in the bottom of my toolbox, preferring other more purpose-built tools whenever possible.

With that in mind I would use the DateTime class to accomplish this.

anykeyNode.InnerText = DateTime.Parse(anykeyNode.InnerText).ToShortDateString();
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • I am of the opinion that Regex is ideal when attempting to capture large sets of information or doing replacements in big strings/validations. Do you think you'd agree with me on this? Also, I too, don't think a case this simple required Regex. – Tiramonium Oct 16 '19 at 19:59
  • 1
    Regex is ideal whenever you are capturing patterns, large or small. Its up to you as the programmer to decide when using a regex is more complicated than other means. If you're going to use DateTime for this, use TryParse instead so this doesn't blow up with exceptions. Or at the very least use the safe navigation operator on the call to `ToShortDateString` – DetectivePikachu Oct 16 '19 at 20:19
  • @DetectivePikachu I have a huge string, it is not only a small line – s shah Oct 16 '19 at 20:33
  • @Tiramonium: I absolutely disagree with your statements. Capturing "large sets of information" and "doing replacements in big strings" is not what RegEx is all about. RegEx is about **searching for patterns within strings**. The language of RegEx does not specify a lower or upper bound on the size of these strings. – Sam Axe Oct 16 '19 at 20:43
  • @SamAxe I meant it in a practical sense. Sure, before doing literally anything you need to build a expression that matches **something**, but what you can do with your matches is pretty straightforward. Also, about the size of the searched value, I also meant it from the practical standpoint. Small values are more easily manipulated by traditional ways, but large ones not so much. That's where Regex comes in. – Tiramonium Oct 17 '19 at 01:21
  • @SamAxe so far in my career, the only people I've ever seen hating regular expressions did it only for the sake of hating. No logic or reason behind it. Either the full realm of possibilities with Regex isn't/wasn't completely understood by some, or it's just a old tale being kept alive by the ancient ones, lol. – Tiramonium Oct 17 '19 at 01:32