nushell / nushell

A new type of shell
https://www.nushell.sh/
MIT License
31.73k stars 1.63k forks source link

`str contains`: add flag for exact word match like `grep -w` #12390

Open Schweber opened 5 months ago

Schweber commented 5 months ago

Related problem

I was exploring ways to check for the existence of a search term in a string consisting of several words in nu. I want to know if the search term matches one of the words in the string exactly.

However, str contains does not have a flag to match whole words like grep -w and i didn't find another nu command that can do what i want out-of-the-box.

Describe the solution you'd like

I would like either 1) str contains to receive a flag that does a wordwise exact match 2) or another command that has this functionality

Describe alternatives you've considered

I could do find -r \bsearchterm\b and check its output with an if statement or something like that. Or i could do $string | split row ' ' | any {$in == searchterm}.

It would be nice to have a simpler alternative to this though.

devyn commented 5 months ago

How about a regex condition? Not that I disagree with adding the flag

$string =~ '\bsearchterm\b'
Schweber commented 5 months ago

Ah thanks, i didn't know that i can do it this simply.

fdncred commented 5 months ago

Many people forget that !~ and =~ are regex operators. You can see that with help operators.