正则表达式匹配空字符串或给定集合中的字符串

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

Regular expression matching either an empty string or a string in a given set

问题

I would like to match either "direction" (">" or "<" ) from a string like "->", "<==", "...". When the string contains no direction, I want to match "". More precisely, the equivalent Python expression would be:

  1. ">" if ">" in s else ("<" if "<" in s else "")

I first came up with this simple regular expression:

  1. re.search(r"[<>]|\"", s)[0]

... but it evaluates to "" as soon as the direction is not in the first position. How would you do that?

英文:

I would like to match either "direction" (&quot;&gt;&quot; or &quot;&lt;&quot;) from a string like &quot;-&gt;&quot;, &quot;&lt;==&quot;, &quot;...&quot;. When the string contains no direction, I want to match &quot;&quot;. More precisely, the equivalent Python expression would be:

  1. &quot;&gt;&quot; if &quot;&gt;&quot; in s else (&quot;&lt;&quot; if &quot;&lt;&quot; in s else &quot;&quot;)

I first came up with this simple regular expression:

  1. re.search(r&quot;[&lt;&gt;]|&quot;, s)[0]

... but it evaluates to &quot;&quot; as soon as the direction is not in the first position. How would you do that?

答案1

得分: 4

"A simpler version of Andrej Kesely's solution:

  1. re.search('<|>|$', s)[0]
  2. ```"
  3. <details>
  4. <summary>英文:</summary>
  5. A simpler version of [Andrej Kesely](https://stackoverflow.com/users/10035985)&#39;s solution:
  6. ```python
  7. re.search(&#39;&lt;|&gt;|$&#39;, s)[0]

答案2

得分: 3

Here's the translated content without the code:

如果你想要一个纯粹的 re 解决方案(regex101):

打印:

这将匹配第一个 &lt;&gt; 字符串或字符串的末尾 $(然后返回空字符串 &#39;&#39;)。

编辑:简化了正则表达式,感谢 @InSync

英文:

If you want pure re solution (regex101):

  1. import re
  2. test_cases = [
  3. &quot;-&gt;&quot;, &quot;&lt;==&quot;, &quot;...&quot;
  4. ]
  5. for t in test_cases:
  6. print(re.search(&#39;(?=[&lt;&gt;]).|$&#39;, t)[0])

Prints:

  1. &gt;
  2. &lt;

This will match first &lt; or &gt; string or end of string $ (then it returns empty string &#39;&#39;)


EDIT: Simplified the regex, thanks @InSync

答案3

得分: 2

The reason this regex [&lt;&gt;]| returns "" if the first character is not < or > is because the alternation in this case always matches the position, returning a zero length match for re.search

Instead of writing if/else statements or a regex, you could also use next() which can return a default value and list the strings that you want to match in an array:

test_str = "this is a test string with > and <"
items = ["<", ">"]
item = next((i for i in items if i in test_str), "")
print(item)

英文:

The reason this regex [&lt;&gt;]| returns "" if the first character is not &lt; or &gt; is because the alternation in this case always matches the position, returning a zero length match for re.search

Instead of writing if/else statements or a regex, you could also use next() which can return a default value and list the strings that you want to match in an array:

  1. test_str = &quot;this is a test string with &gt; and &lt;&quot;
  2. items = [&quot;&lt;&quot;, &quot;&gt;&quot;]
  3. item = next((i for i in items if i in test_str), &quot;&quot;)
  4. print(item)

huangapple
  • 本文由 发表于 2023年5月14日 02:59:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244436.html
匿名

发表评论

匿名网友

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

确定