0

I need to replace all the dots (.) to "[DOT]" available in the html tags and not in the outside of the tags. i.e. attribute value which contains dot (.) should be replace as "[DOT]" but not inner text.

example tag

<link rel="stylesheet" href="27674557W.patent.001_files/27674557W.patent.001.css" type="text/css"/>

changes should be like:

<link rel="stylesheet" href="27674557W[DOT]patent[DOT]001_files/27674557W[DOT]patent[DOT]001[DOT]css" type="text/css"/>

I have tried this patterns in regex.

<(?:[^\.>]*)([\.])(?:[^>]*)>

Replacing code in c# is:

string inputText = <tagText>;
string pattern = @"<([^\.>]*)([\.])([^>]*)>";
inputText = Regex.Replace(inputText, pattern, "$1[DOT]$3", RegexOptions.Singleline);

The above code only replace the first dot in the tab, remaining dots are not changing. I need to change in single shot without using any loop in c#.

Note: Only to replace inside the angle bracket. Not in innertext.

Thanks.

Bala
  • 102
  • 8

4 Answers4

1

You could try using string.Replace().

inputText = inputText.Replace(@".","[DOT]");
Jaime Slater
  • 43
  • 1
  • 2
  • 7
1

why not simply use new Regex("\\.")? Works for me, see fiddle

However It´s a bad idea to parse HTML with regex. In your case you should use an HTML-parser beforehand, and then extract the attribute href. Now you can continue with your regex:

var attribute = htmlparser.GetAttribute("href");
var result = r.Replace(attribute, "[DOT]");

or even simpler as Jaime also answered without a regex at all.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • It will replace all dots available in that html content. But i need to change only the dots available inside the angle brackets. – Bala Dec 22 '17 at 09:48
1

You can use this regex to replace . inside the html tags:

(?<=<[^>]+)\.(?=[^>]*>)
karthik selvaraj
  • 426
  • 5
  • 12
0

You can use this regex:

<[a-z]+\s.*(\\.)?.*>

the first character set is for the tag name, next is a whitespace then follow any characters then a dot then any other characters

Vikramark
  • 137
  • 13