在文本中添加换行,仅保留出现在字符串末尾的标签。

huangapple go评论119阅读模式
英文:

Add newlines to separate text and only hashtags that appear at the end of the string

问题

I need an enter line before the hashtags start (so there is some space).

Example:

> It's essential to keep your pup cool in the summer months! Try to keep
> them out of the heat as much as possible - keep the house cool,
> provide shade and run a fan for them if you can. #Dogs #Animals
> #Summer #DogCare #Humidity #HeatStroke #Cooling #HeatExhaustion #StayCool #HealthyPups

Should be:

> It's essential to keep your pup cool in the summer months! Try to keep
> them out of the heat as much as possible - keep the house cool,
> provide shade and run a fan for them if you can.

>

#Dogs #Animals #Summer #DogCare #Humidity #HeatStroke #Cooling #HeatExhaustion #StayCool #HealthyPups

If there is a hashtag in the text it should not add whitespace, for example (note hashtags in text!):

> It's essential to keep your pup cool in the summer months! Try to keep
> them out of the heat as much as #possible - keep the house cool,
> #provide shade and run a #fan for them if #you can. #Dogs #Animals #Summer #DogCare #Humidity #HeatStroke #Cooling #HeatExhaustion #StayCool #HealthyPups

如何使用正则表达式和PHP解决这个问题?

英文:

I've already tried a couple of regex but cannot fix this one:

I need an enter line before the hashtags start (so there is some space).

Example:

> It's essential to keep your pup cool in the summer months! Try to keep
> them out of the heat as much as possible - keep the house cool,
> provide shade and run a fan for them if you can. #Dogs #Animals
> #Summer #DogCare #Humidity #HeatStroke #Cooling #HeatExhaustion #StayCool #HealthyPups

Should be:

> It's essential to keep your pup cool in the summer months! Try to keep
> them out of the heat as much as possible - keep the house cool,
> provide shade and run a fan for them if you can.
>
> #Dogs #Animals #Summer #DogCare #Humidity #HeatStroke #Cooling #HeatExhaustion #StayCool #HealthyPups

If there is a hashtag in the text it should not add whitespace, for example (note hashtags in text!):

> It's essential to keep your pup cool in the summer months! Try to keep
> them out of the heat as much as #possible - keep the house cool,
> #provide shade and run a #fan for them if #you can. #Dogs #Animals #Summer #DogCare #Humidity #HeatStroke #Cooling #HeatExhaustion #StayCool #HealthyPups

How can I solve this with Regex and PHP?

答案1

得分: 2

你可以使用正则表达式来匹配一系列的哈希标签:

(?:#\w+\s*)+$
  • #\w+ 匹配单个哈希标签
  • \s* 匹配哈希标签之间的可选空白
  • + 在分组后匹配至少一个这样的序列
  • $ 匹配输入字符串的末尾。

然后,你可以使用 preg_replace() 在这之前插入一个空行:

$text = preg_replace('/((?:#\w+\s*)+)$/', "\n\n$1", $text);
英文:

You can match a series of hashtags with the regexp:

(?:#\w+\s*)+$
  • #\w+ matches a single hashtag
  • \s* matches the optional whitespace between the hashtags
  • + after the group matches a sequence of at least one of these
  • $ matches the end of the input string.

You can then use preg_replace() to insert a blank line before this.

$text = preg_replace('/((?:#\w+\s*)+)$/', "\n\n$1", $text);

答案2

得分: 1

匹配一个空格,其后跟随一个或多个哈希标签序列,一直到字符串末尾。通过在第四个参数中添加 1 来通知 preg_replace() 仅进行一次替换。

代码: (演示)

echo preg_replace('/ (?=(?:#[a-z]+\s*)+$)/i', "\n\n",  $text, 1);

或者不使用前瞻或替换限制:(演示)

echo preg_replace('/ ((?:#[a-z]+\s*)+)$/i', "\n\n$1",  $text);

这两种方法都会将空格替换为两个换行符,位于哈希标签序列之前。

英文:

Match a space which is followed by one or more sequences of hashtags until the end of the string. Inform preg_replace() to make only 1 replacement by adding 1 as the fourth parameter.

Code: (Demo)

echo preg_replace('/ (?=(?:#[a-z]+\s*)+$)/i', "\n\n",  $text, 1);

Or without a lookahead or a replacement limit: (Demo)

echo preg_replace('/ ((?:#[a-z]+\s*)+)$/i', "\n\n$1",  $text);

Both approaches will cleanly replace the space before the sequence of hashtags with the two newlines.

huangapple
  • 本文由 发表于 2023年6月1日 02:33:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定