如何只替换一次子字符串?

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

How to replace a substring only once?

问题

你可以使用以下正则表达式来匹配并替换第二次出现的Midday Routines

^(.*?,.*?),.*?(Midday Routines)

然后替换为:

$1,⚡ $2

这将匹配第一和第二个逗号之间的文本,然后在第二次出现的Midday Routines前添加

请注意,这个正则表达式假定你的输入字符串的格式保持一致,以便正常工作。

英文:

I have the following string:

Log First Meal Time,Twitter,Midday Routines Done,Midday Routines

I want to write a regular expression such that I will get exactly the following:

Log First Meal Time,Twitter,Midday Routines Done,⚡ Midday Routines

How to do it with some Regex that will only match the second occurrence of Midday Routines in this case?

I was replacing the Midday Routines with ⚡ Midday Routines, but it seems iPhone Shortcuts by default replace all occurrences, and I got:

Log First Meal Time,Twitter,⚡ Midday Routines Done,⚡ Midday Routines

I am new to regular expression and I have no ideas...

Please help! Thank you in advance.

答案1

得分: 1

在iOS Shortcuts中,您应该能够使用ICU正则表达式

对于匹配,请使用:(Midday Routines)(.*?)\1

对于替换,请使用:$1$2⚡ $1

英文:

In IOS Shortcuts, you should be able to use ICU Regex.

For Match, use: (Midday Routines)(.*?)\1

For Replace, use: $1$2⚡ $1

答案2

得分: 0

以下是您要翻译的代码部分:

你的问题没有明确定义从你的评论中看你想在字符串中最后一个逗号之后插入一个字符

你可以使用正向先行断言来实现

(?<=,)(?=[^,]+$)

解释:
- `(?<=,)` -- 逗号的正向回顾断言
- `(?=[^,]+$)` -- 直到字符串末尾的任何非逗号的正向预查

你没有指定编程语言。在JavaScript中,你可以使用 replace 方法来实现:

'Log First Meal Time,Twitter,Midday Routines Done,Midday Routines'.replace(/(?<=,)(?=[^,]+$)/, '⚡ ')
// 返回: 'Log First Meal Time,Twitter,Midday Routines Done,⚡ Midday Routines'


***了解更多关于正则表达式的信息:*** https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex

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

Your question is not well defined. From you comment it looks like you want to insert a character after the last comma in the string.

You can do that with with lookarounds:

(?<=,)(?=[^,]+$)

Explanation:
- `(?&lt;=,)` -- positive lookbehind for a comma
- `(?=[^,]+$)` -- positive lookahead for anything not a comma until end of string

You did not specify the language. In JavaScript you would do a replace:

'Log First Meal Time,Twitter,Midday Routines Done,Midday Routines'.replace(/(?<=,)(?=[^,]+$)/, '⚡ ')
// returns: 'Log First Meal Time,Twitter,Midday Routines Done,⚡ Midday Routines'


***Learn more about regex:*** https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex


</details>



# 答案3
**得分**: 0

在Java中,你可以使用`String.replaceFirst`来实现这个操作。下面是代码示例:

```java
String s = "Log First Meal Time,Twitter,Midday Routines Done,Midday Routines"; 
s = s.replaceFirst("(Midday Routines.*?)(Midday Routines)", "$1⚡ $2");
System.out.println(s);

这段代码会将第一个出现的"Midday Routines"和它之前的内容替换为"⚡ Midday Routines",无论后面跟着什么内容,最终输出的结果如下:

Log First Meal Time,Twitter,Midday Routines Done,⚡ Midday Routines
英文:

You didn't specify a language. Here is one way to do it in Java.

Use String.replaceFirst.

  • () capture group
  • .*? - reluctant quantifier
  • $n back reference to the captured strings.
  • First capture group is the first occurrence plus everything just before the second occurrence.
  • Second capture group is the second occurrence.
String s = &quot;Log First Meal Time,Twitter,Midday Routines Done,Midday Routines&quot;; 
s = s.replaceFirst(&quot;(Midday Routines.*?)(Midday Routines)&quot;, &quot;$1\u26A1 $2&quot;);
System.out.println(s);

prints

Log First Meal Time,Twitter,Midday Routines Done,⚡ Midday Routines

The above will work regardless of what comes after your initial string.

答案4

得分: -1

以下是翻译后的内容:

根据我的理解,您想要匹配最后一个逗号之后的字符串。

以下是正则表达式:

(?<=,)[^,]*$

解释:

  • (?<=,):回顾先验,验证在字符串之前是否有逗号。
  • [^,]*:否定字符类,带有限定符,指定“匹配除了逗号之外的其他字符多次”。
  • $:锚点,指定此模式应该结束句子。

因此,这个模式确保了字符串之前有一个逗号,之后没有逗号,并且这是在句子的末尾。这样,您只匹配最后一个没有逗号的字符串。

在此处验证

英文:

From what I understand, you want to match the last string after the last comma.

Here's the regex:

(?&lt;=,)[^,]*$

Explanation:

  • (?&lt;=,): lookbehind that verifies that there's a comma before the string
  • [^,]*: negated character class with quantifier that specifies - "match every other characters multiple times except a comma"
  • $: anchor that specifies that this pattern should end the sentence

So this pattern ensures that a comma precedes the string, and there's no comma after the preceded comma, and this is at the end of the sentence. This way, you match only the last string that doesn't have any comma after it.

Verify here

huangapple
  • 本文由 发表于 2023年2月24日 00:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547701.html
匿名

发表评论

匿名网友

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

确定