skip to Main Content

Test Input

<!DOCTYPE html>
<html>
  <head>
    <title>This is a title</title>
  <head>
  <body>
    <div>
        <p>H/e/l/l/o abcdefgw/o/r/l/d!</p>
    </div>
  <body>
<html>

Test Output

<!DOCTYPE html>
<html>
  <head>
    <title>This is a title</title>
  </head>
  <body>
    <div>
        <p>H/e/l/l/o abcdefgw/o/r/l/d!</p>
    </div>
  </body>
</html>

We do not want to replace every single forward slash with backslash /.

Some the of backslashes are intended to be backslashes.

We wish only to replace backslash with forward slash / if the backslash is inside of a mispelled HTML tag such as <kbd>

2

Answers


  1. you can use the folloing pattern:

    (?<=<)\(?=.*?>)
    

    code online

    Login or Signup to reply.
  2. You can use regular expressions

    import re
    
    
    def convert_backslash(text):
        return re.sub(r'<\', r'</', text, re.MULTILINE)
    

    Text input must be a raw string like r"…"

    text_input = r"""
    <!DOCTYPE HTML>
    <HTML>
      <head>
        <title>This is a title</title>
      <head>
      <body>
        <div>
            <p>H/e/l/l/o abcdefgw/o/r/l/d!</p>
        </div>
      <body>
    <html>"""
    
    print(convert_backslash(text_input))
    

    Output:

    <!DOCTYPE html>
    <html>
      <head>
        <title>This is a title</title>
      </head>
      <body>
        <div>
            <p>H/e/l/l/o abcdefgw/o/r/l/d!</p>
        </div>
      </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search