1

I am dealing with a bunch of xml files that contain one-line-comments like this: // Some comment. I am pretty sure that xml comments look like this: <!-- Some comment --> I would like to use a regular expression in the Atom editor to find and replace all wrong comment syntax.

according to this question, the comment can be found with (?<=\s)//([^\n\r]*) and replaced with something like <!--$1-->. There must be an error somewhere since clicking replace button leaves the comment as is, instaed of replacing it. Actually I can't even replace it with a simple character. The find and replace works with a different regex in the "Find" field:
Find: name.*
Replace: baloon

Is there anything I can write in the "Find" and "Replace" field to achieve this transformation?

1
  • 1
    If it is Atom, the problem might be with the lookbehind. Try just //([^\n\r]+) and replace wih <!--$1-->. If you need to check for the space, try (^|\s)//([^\n\r]+) => $1<!--$2--> Commented Apr 6, 2020 at 12:02

1 Answer 1

1

Atom editor search and replace currently does not support lookbehind constructs, like (?<=\s). To "imitate" it, you may use a capturing group with an alternation between start of string, ^, and a whitespace, \s.

So, you may use

Find What: (^|\s)//([^\n\r]+)
Replace With: $1<!--$2-->

See the regex demo. NOTE \s may match newlines, so you may probably want to use (^|[^\S\r\n])//([^\n\r]+) to avoid matching across line breaks.

If you do not need to check for a whitespace, just remove that first capturing group and use a mere:

Find What: //([^\n\r]+)
Replace With: <!--$1-->

See another regex demo.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.