正则表达式检查字符串中出现的奇数次数

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

Regex to check odd number of occurance in string

问题

我想制作一个Java正则表达式,以删除字符串“4040”的前后内容,考虑以下情况。

场景:1

如果整个字符串中包含偶数对4040,则将删除每一对,并且输出将是空字符串。

示例:

  • 4040404040404040 --> ""
  • 40404040 --> ""

场景:2

在其他情况下,在4040对之间可能会有一些数据,然后应该返回在删除两侧的4040之后的字符串。两侧的对数可以是任意的(也可以为0)。

示例:

  • 4040StackOverFlow4040 --> StackOverFlow
  • 4040StackOverFlow40404040 --> StackOverFlow
  • StackOverFlow4040 --> StackOverFlow
  • 40404040StackOverFlow --> StackOverFlow

场景:3

如果我有奇数对4040,则应返回“4040”,因为中间的那个是实际数据。

示例:

  • 404040404040 --> 4040
  • 40404040404040404040 --> 4040

需要一个正则表达式,可以包括所有这些情况。

我尝试通过匹配字符串使用 ^(4040)*|(4040)*$ 并将其替换为空字符串来实现。但这不适用于第三种情况。有没有办法检测到奇数对4040?

(Note: I've provided the translation as requested without answering the translation question again.)

英文:

I want to make java regex to remove string "4040" before and after string, considering below scenarios.

scenario : 1

If whole string having even pair of 4040 then it will remove each pair and output will be empty string.

example :

  • 4040404040404040 --> ""
  • 40404040 --> ""

scenario : 2

In other case there can be some data in-between pairs of 4040 then it should return that string after removing 4040 from both size. No of pair both side can be any (also can be 0).

example:-

  • 4040StackOverFlow4040 --> StackOverFlow
  • 4040StackOverFlow40404040 --> StackOverFlow
  • StackOverFlow4040 --> StackOverFlow
  • 40404040StackOverFlow --> StackOverFlow

scenario : 3

If I having odd no of pairs for 4040 the it should return "4040". because middle one is actual data.

example:-

  • 404040404040 --> 4040
  • 40404040404040404040 --> 4040

Need one regex which can include all scenarios.

I tried by matching string using ^(4040)*|(4040)*$ and replace it with empty string. But it is not working for third scenario. Any way to detect odd number of pairs of 4040.

答案1

得分: 1

为什么要使用正则表达式?只需简化生活:

(伪代码)

var string = ...
while (string.startsWith("4040") && string.endsWith("4040")) {
  string = string.substring(4, string.length - 4)
}
return string
英文:

Why use a regex? Just make life easy for yourself:

(pseudo code)

var string = ...
while (string.startsWith("4040") && string.endsWith("4040")) {
  string = string.substring(4, string.length - 4)
}
return string

答案2

得分: 1

尝试这个:

str = str.replaceAll("^((?:4040)+)(.*?)\$", "$2");

这段代码通过使用后向引用 \1 来要求输入的前半部分和后半部分相同。中间部分被捕获为第二组(group 2),用于替换整个字符串,有效地“提取”它。

英文:

Try this:

str = str.replaceAll("^((?:4040)+)(.*?)\$", "$2");

This works by requiring the same input front and back by way of the back reference \1 to the front. The middle is captured as group 2, which is used to replace the entire string, effectively "extracting" it.

答案3

得分: 1

你可以使用竖线来在不同的情况之间交替

^(4040)(?:40404040)*$|^(?:4040)+|(?:4040)+$

在regex101上查看演示(替换为$1捕获第一个组的内容)

  1. ^(4040)(?:40404040)*$ 如果整个字符串由奇数个 4040 组成,将会捕获第一次出现并替换字符串为捕获的值。
  2. |^(?:4040)+ 或者匹配一个或多个出现在开头
  3. |(?:4040)+$ 或者匹配一个或多个出现在字符串的末尾

为了重复子字符串,使用了(?:非捕获组)

英文:

You can use the vertical bar to alternate between different scenarios:

^(4040)(?:40404040)*$|^(?:4040)+|(?:4040)+$

See this demo at regex101 (replace to $1, capture of the first group)

  1. ^(4040)(?:40404040)*$ If all string is composed from an uneven amount of 4040<br> this will capture the first occurance and subsitute the string to the captured value.
  2. |^(?:4040)+ OR match one or more occurances at the start
  3. |(?:4040)+$ OR match one or more at the end of the string

For repetition of the substrings (?: non capturing groups ) are used.

huangapple
  • 本文由 发表于 2023年6月19日 16:11:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504771.html
匿名

发表评论

匿名网友

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

确定