正则表达式找到两侧没有空格的&符号

时间:2023-01-15 17:34:14

I'm just trying to figure out the regex to find any ampersands that aren't immediately preceded and followed by a space. For example, it would find "asdf&asdf" and "asdf& asdf" but not "asdf & asdf"

我只是试图找出正则表达式,找到任何不会立即前后跟空格的&符号。例如,它会找到“asdf&asdf”和“asdf&asdf”但不会找到“asdf&asdf”

This will be used in a preg_replace to add spaces before and after. (If you're wondering, the problem is that I'm stuck with a WYSIWYG that has a bug where it strips spaces on both sides of ampersands, and I need to manually add them back after the fact). Thoughts?

这将在preg_replace中用于在前后添加空格。 (如果你想知道,问题是我遇到了一个带有错误的WYSIWYG,它会在&符号的两侧剥去空格,我需要在事后添加它们)。思考?

8 个解决方案

#1


4  

Ampersand width not after a space or ampersand not before a space, but not both:

&符号宽度不是在空格之后,也不是在空格之前,而不是两者:

(?<!\s)&|&(?!\s)

Using lookaround, so this captures the ampersand only.

使用环视,因此仅捕获&符号。

#2


5  

(\S&\S|\s&\S|\S&\s)
  • Non whitespace char, followed by & and another non space char
    OR
  • 非空白字符,后跟&和另一个非空格字符OR

  • Whitespace followed by &, followed by non space char
    OR
  • 空格后跟&,后跟非空格字符OR

  • Non whitespace char followed by &, followed by another whitespace char
  • 非空格字符后跟&,后跟另一个空格字符

#3


1  

I suggest to first find all ampersands independent of the characters to the left and right and then use a negative look behind assertion to ensure that not both characters are spaces.

我建议首先找到所有与左右字符无关的&符号,然后在断言后面使用负面看法,以确保两个字符都不是空格。

.&.(?<! & )

#4


1  

Ampersand with non-space after or before:

在之前或之后具有非空格的&符号:

(&[^ ]|[^ ]&)

#5


1  

To put space if it is not there on either side, can be done like this

如果两边都没有空间,可以这样做

echo preg_replace('/ ?& ?/',' & ','asdf&asdf asdf& asdf asdf & asdf');
//asdf & asdf asdf & asdf asdf & asdf

For without spaces on both sides (Answering to question title), it will fail on asdf& asdf

因为双方都没有空格(回答问题标题),它将在asdf和asdf上失败

echo preg_replace('/(?<! )&(?! )/',' & ','asdf&asdf asdf& asdf asdf & asdf');
//asdf & asdf asdf& asdf asdf & asdf

#6


0  

It should work (not tested) : /([^\s]&|&[^\s])/. If you are only concerned about spaces and not "any blank character" : /([^ ]&|&[^ ])/

它应该工作(未测试):/([^ \ s]&|&[^ \ s])/。如果您只关心空格而不是“任何空白字符”:/([^]&|&[^])/

#7


0  

([^\s]+&.+)|(.+&[^\s]+)

This regex makes sure that there is no space on the left xor the right of an & character

这个正则表达式确保左侧和/字符右侧没有空格

#8


0  

(?<=\s)\&\&\s

https://regex101.com/r/vBuuUf/1

hope it helps

希望能帮助到你

#1


4  

Ampersand width not after a space or ampersand not before a space, but not both:

&符号宽度不是在空格之后,也不是在空格之前,而不是两者:

(?<!\s)&|&(?!\s)

Using lookaround, so this captures the ampersand only.

使用环视,因此仅捕获&符号。

#2


5  

(\S&\S|\s&\S|\S&\s)
  • Non whitespace char, followed by & and another non space char
    OR
  • 非空白字符,后跟&和另一个非空格字符OR

  • Whitespace followed by &, followed by non space char
    OR
  • 空格后跟&,后跟非空格字符OR

  • Non whitespace char followed by &, followed by another whitespace char
  • 非空格字符后跟&,后跟另一个空格字符

#3


1  

I suggest to first find all ampersands independent of the characters to the left and right and then use a negative look behind assertion to ensure that not both characters are spaces.

我建议首先找到所有与左右字符无关的&符号,然后在断言后面使用负面看法,以确保两个字符都不是空格。

.&.(?<! & )

#4


1  

Ampersand with non-space after or before:

在之前或之后具有非空格的&符号:

(&[^ ]|[^ ]&)

#5


1  

To put space if it is not there on either side, can be done like this

如果两边都没有空间,可以这样做

echo preg_replace('/ ?& ?/',' & ','asdf&asdf asdf& asdf asdf & asdf');
//asdf & asdf asdf & asdf asdf & asdf

For without spaces on both sides (Answering to question title), it will fail on asdf& asdf

因为双方都没有空格(回答问题标题),它将在asdf和asdf上失败

echo preg_replace('/(?<! )&(?! )/',' & ','asdf&asdf asdf& asdf asdf & asdf');
//asdf & asdf asdf& asdf asdf & asdf

#6


0  

It should work (not tested) : /([^\s]&|&[^\s])/. If you are only concerned about spaces and not "any blank character" : /([^ ]&|&[^ ])/

它应该工作(未测试):/([^ \ s]&|&[^ \ s])/。如果您只关心空格而不是“任何空白字符”:/([^]&|&[^])/

#7


0  

([^\s]+&.+)|(.+&[^\s]+)

This regex makes sure that there is no space on the left xor the right of an & character

这个正则表达式确保左侧和/字符右侧没有空格

#8


0  

(?<=\s)\&\&\s

https://regex101.com/r/vBuuUf/1

hope it helps

希望能帮助到你