不抓人是不是贪心?

时间:2022-12-14 12:15:02

I want to remove everything after the first

我想把第一个-之后的所有东西都去掉

Sample:

示例:

Yes – No – No – No
Yes – No – No & No – No
Yes Yes – No & No – No
Yes – No – No & No 
Yes – No
Yes – No – No

Regex I am using:

正则表达式我用:

/(.+) (?:– .+)/

Match:

匹配:

`Yes – No – No`
`Yes – No – No & No`
`Yes Yes – No & No`
`Yes – No`
`Yes`
`Yes – No`

It seems like the non-capturing group is lazy rather than greedy?

看起来不抓人的人是懒惰而不是贪婪?

My desired result is:

我想要的结果是:

`Yes`
`Yes`
`Yes Yes`
`Yes`
`Yes`
`Yes`

4 个解决方案

#1


2  

(.+?) (?:– .+)

This will give the desired result.Your regex (.+) (?:– .+) is incorrect as .+ will try to match as many characters as possible before stopping, so it will stop at the last -. Instead, use .+? to make it lazy.

这将得到预期的结果。你的正则表达式(+)(?: -. +)是不正确的。+将在停止之前尽可能多地匹配字符,所以它将在最后一个-停止。相反,使用。+ ?懒惰。

http://regex101.com/r/hS3dT7/3

http://regex101.com/r/hS3dT7/3

#2


1  

Actually it's the first (capturing) group being greedy that is the problem. The regular expression engine will try to fill the leftmost part of the pattern first. Try making the first group lazy:

实际上,第一个(捕捉)组的贪婪是问题所在。正则表达式引擎将首先尝试填充模式的最左边部分。试着让第一个小组偷懒:

/(.+?) (?:– .+)/

On the other hand, you don't really need a group for the second bit anyway, and can just capture up to the first - (with optional whitespace before it) and be done with it:

另一方面,对于第二比特,您实际上并不需要一个组,只需捕获第一个——(前面有可选的空格),然后进行处理:

/^(.*?)\s*-/

Or if you don't care about whitespace or want to trim afterward, it's simply:

或者,如果你不关心空白或想在之后修剪,它很简单:

/^([^-]*)/

#3


0  

Use the below regex and remove the matched strings.

使用下面的regex并删除匹配的字符串。

^[^–]*\K\s+–.*

DEMO

演示

#4


0  

Another simple solution, although it is not recommended in production code, since it may incur performance hit if the string contains a long streak of consecutive spaces but no dash. (The same problem also occurs when you write [ \t]+$ to remove trailing spaces and tabs).

另一个简单的解决方案,尽管在生产代码中不推荐使用它,因为如果字符串包含长串连续空格,但没有破折号,那么它可能会导致性能下降。(当您编写[\t]+$删除尾随空格和制表符时,也会出现同样的问题)。

Search with this regex (the separator / is added for clarity):

使用此regex进行搜索(为了清晰起见,添加了分隔符/):

/ *–.*/

And replace with empty string.

用空字符串替换。

DEMO

演示

The idea is very simple. We just search for the first dash, which may be preceded by spaces, and remove them along with everything that comes after them.

这个想法很简单。我们只是搜索第一个破折号,它可能会在前面加上空格,然后把它们连同后面的所有东西一起删除。

#1


2  

(.+?) (?:– .+)

This will give the desired result.Your regex (.+) (?:– .+) is incorrect as .+ will try to match as many characters as possible before stopping, so it will stop at the last -. Instead, use .+? to make it lazy.

这将得到预期的结果。你的正则表达式(+)(?: -. +)是不正确的。+将在停止之前尽可能多地匹配字符,所以它将在最后一个-停止。相反,使用。+ ?懒惰。

http://regex101.com/r/hS3dT7/3

http://regex101.com/r/hS3dT7/3

#2


1  

Actually it's the first (capturing) group being greedy that is the problem. The regular expression engine will try to fill the leftmost part of the pattern first. Try making the first group lazy:

实际上,第一个(捕捉)组的贪婪是问题所在。正则表达式引擎将首先尝试填充模式的最左边部分。试着让第一个小组偷懒:

/(.+?) (?:– .+)/

On the other hand, you don't really need a group for the second bit anyway, and can just capture up to the first - (with optional whitespace before it) and be done with it:

另一方面,对于第二比特,您实际上并不需要一个组,只需捕获第一个——(前面有可选的空格),然后进行处理:

/^(.*?)\s*-/

Or if you don't care about whitespace or want to trim afterward, it's simply:

或者,如果你不关心空白或想在之后修剪,它很简单:

/^([^-]*)/

#3


0  

Use the below regex and remove the matched strings.

使用下面的regex并删除匹配的字符串。

^[^–]*\K\s+–.*

DEMO

演示

#4


0  

Another simple solution, although it is not recommended in production code, since it may incur performance hit if the string contains a long streak of consecutive spaces but no dash. (The same problem also occurs when you write [ \t]+$ to remove trailing spaces and tabs).

另一个简单的解决方案,尽管在生产代码中不推荐使用它,因为如果字符串包含长串连续空格,但没有破折号,那么它可能会导致性能下降。(当您编写[\t]+$删除尾随空格和制表符时,也会出现同样的问题)。

Search with this regex (the separator / is added for clarity):

使用此regex进行搜索(为了清晰起见,添加了分隔符/):

/ *–.*/

And replace with empty string.

用空字符串替换。

DEMO

演示

The idea is very simple. We just search for the first dash, which may be preceded by spaces, and remove them along with everything that comes after them.

这个想法很简单。我们只是搜索第一个破折号,它可能会在前面加上空格,然后把它们连同后面的所有东西一起删除。