Matching a single asterisk that isn't adjacent to another asterisk, using RegEx

StackOverflow https://stackoverflow.com/questions/20668478

  •  19-09-2022
  •  | 
  •  

Вопрос

I'm working with a Filemaker Pro database. Which for some reason won't export XML that is written into the database. It entitizes all the special characters making it difficult to export the embedded XML into an actual XML document. To combat this I have decided to use a regular expression in a find and replace to target asterisks that I'm putting on both sides of a word or phrase.

Example:

**Word to put tags around*

I have a regular expression that matches two asterisks that are adjacent. But I cant remember how to match a single asterisks that IS NOT adjacent to another. Once I can target one and then the other separately I will find and replace them with the appropriate tag.

Это было полезно?

Решение

If it supports negative lookarounds, you can use just that:

(?<!\*)\*(?!\*)

That will match single asterisks only.

(?<!\*) prevents matches if preceded by an asterisk.

(?!\*) prevents matches if followed by an asterisk.

Другие советы

Another option would be to match the * and the surrounding non-*.

([^*]|^)[*]([^*]|$)

Regular expression visualization

You could use this for example to replace single * to ** like this

s.replace("([^*]|^)[*]([^*]|$)","$1**$2");
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top