在最后一个字符之前插入破折号的正则表达式是:

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

Regular Expression to insert a dash before the final character?

问题

Jan104ac-a

英文:

Totally new at this and no search has been able to help me.

Trying to turn:

Jan104aca

Into:

Jan104ac-a

/(?<=.)(?!$)

Gets me to every space, just not sure how to isolate the final space

Thanks!

答案1

得分: 2

你可以尝试:

(.*)(.)

并将其替换为:

-

正则表达式演示。


<details>
<summary>英文:</summary>

You can try:

```none
(.*)(.)

and substitute it for:

-

Regex demo.

答案2

得分: 1

Find: (.)$
Replace: -$1

演示

英文:

You may try the following find and replace, in regex mode:

Find:    (.)$
Replace: -$1

<h2>Demo</h2>

答案3

得分: 1

以下是要翻译的内容:

Search for:

(?=.$)

Replace with:

-

Demo: https://regex101.com/r/LfK29V/1

英文:

You can use a lookahead pattern to assert that a match is followed by a character and the end of the line/string:

Search for:

(?=.$)

Replace with:

-

Demo: https://regex101.com/r/LfK29V/1

答案4

得分: 0

(?< = .)(?=.$)

或者匹配单个字符,然后在替换中使用完全匹配的字符,后跟“-”。

. (?=.$)

英文:

You are almost there, but you have to assert a character followed by the end of the string.

In the replacement use -

(?&lt;=.)(?=.$)

Regex demo.

Or match the single character and only use the lookahead. Then use the full match followed by - in the replacement.

.(?=.$)

Regex demo

huangapple
  • 本文由 发表于 2023年3月23日 09:39:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818607.html
匿名

发表评论

匿名网友

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

确定