Regular expression in case statement [duplicate] - bash

This question already has answers here:
Regular expressions in a Bash case statement
(7 answers)
Closed 9 years ago.
I am trying to filter out some strings using case statement.
case $HOST in
Linux|Windows|Storage*)
I want to filter out the hosts which have names like this
test_prd_linux
test_prd_windows
How can i include *prd* in the above case statment? Something like this?
case $HOST in
Linux|Windows|Storage|*prd*)

These are globs, not regular expressions.
Yes, the glob *prd* will match the cases you have as examples (though I would use the more specific pattern *_prd_* if these examples are representative).
However, you also changed Storage* to Storage, so this will no longer match some strings it used to match. Perhaps put the glob star back.
case $HOST in
Linux|Windows|Storage*|*_prd_*)

Related

Why prepend a character to both strings before comparing for equality with `[[` in bash? [duplicate]

This question already has answers here:
Why do shell script comparisons often use x$VAR = xyes?
(7 answers)
Closed 9 months ago.
I was looking at the source code of NodeSource's installer script for Node.js.
Everywhere in the code, when they want to compare two strings, they prepend X to both sides before comparing:
if [[ "X${NODENAME}" == "XNode.js 10.x" ]]; then
I was pointed to some questions on the network:
What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?
Why do shell script comparisons often use x$VAR = xyes?
Why append an extra character in `test`/`[` string comparison in POSIX sh?
They all explain why this is necessary when comparing with [. But in this case, the comparison is with [[.
Is this technique really necessary for [[?
Credits to #danielhoherd, in a comment
No, it's not necessary.
In fact, this technique is outdated for [ as well, as explained in the SC2268 shellcheck rule:
Avoid x-prefix in comparisons as it no longer serves a purpose.
[...]
Some older shells would get confused if the first argument started with a dash, or consisted of ! or (. As a workaround, people would prefix variables and values to be compared with x to ensure the left-hand side always started with an alphanumeric character.
POSIX ensures this is not necessary, and all modern shells now follow suit.
[...]

Using regular expressions via `re-search-forward` in elisp [duplicate]

This question already has answers here:
Emacs - regular expressions in Lisp need to be double-escaped - why?
(4 answers)
Closed 4 years ago.
I want to search for an regular expression with the function re-search-forward
When I tried using the examples from the page
here: https://www.emacswiki.org/emacs/RegularExpression#toc1
specifically the regular expression \w\{20,\} used to search for a word with 20 letters or more, I get an error.
Here I am placing my cursor after the closing parenthesis in my Lisp buffer and pressing C-x C-e for evaluating it.
However, when I use the Regexp I-search via,
C-M-s it highlights the correct word as expected.
Why is this?
This regexp:
\w\{20,\}
is expressed in a double-quoted elisp string like so:
"\\w\\{20,\\}"
Backslashes are special to the double-quoted read syntax for strings as well as being special to regexp syntax; so if a backslash is for the regexp, you need to double it.

Ruby regex include all alphabets, numbers and / [duplicate]

This question already has answers here:
How to escape all characters with special meaning in Regex
(2 answers)
Closed 7 years ago.
I know this might be asked time and again. But I'm really stuck with this. I've got it to work for including numbers and alphabets but I have no idea on how to include "/" also.
This is what I have,
name.gsub!(/[^0-9A-Za-z]/, '')
So if name is "Cool Stuff *(#/" it returns "CoolStuff". I'd just like it to return "CoolStuff/".
The / is a special character that must be 'escaped' (meaning to take the / literally, and not for a switch or special meaning). So you have:
name.gsub!(/[^0-9A-Za-z]/, '')
But also realize you could shorten your RegEx statement by making it case insensitive by adding an 'i' after the ending slash and therefore allowing you to drop either the [A-Z] or the [a-z] part. So you could have instead:
name.gsub!(/[^0-9A-Z\/]/i, '')
Hope this helps!

Ruby Regex not matching what it should be [duplicate]

This question already has answers here:
How to match all occurrences of a regular expression in Ruby
(6 answers)
Closed 8 years ago.
I've got the following regex:
regex = /\$([a-zA-Z.]+)/
and the following query
query = "Show me the PE Ratio for $AAPL, $TSLA"
Now regex.match(query) should capture AAPL and TSLA, but instead I get the following:
#<MatchData "$AAPL" 1:"AAPL">
which is completely wrong. Anyone know why?
Note that this regex works fine on Rubular: http://rubular.com/r/j0maQHnVFF
In Ruby the .match method will only return the first capture. You need it to return all captured matches, like the /g flag in PCRE
You can use the scan method. The scan method will either give you an array of all the matches or, if you pass it a block, pass each match to the block.
Code
query.scan(/\$([a-zA-Z.]+)/)
Fixed it, needed to use .scan instead of .match

How can one write this gsub regex match? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Perfect way to write a gsub for a regex match?
I am trying to write a gsub for a regex match, but I imagine there's a more perfect way to do this .
My equation :
ref.gsub(ref.match(/settings(.*)/)[1], '')
So that I can take this settings/animals, and return just settings.
But what if settings is null? Than my [1] fails as expected.
So how can one write the above statement assuming that sometimes settings won't match ?
Use /(settings|)(.*)/, then first group will return you "settings" or empty string, if it is not present.
puts 'settings/123'.match(/(settings|)(.*)/)[1];
puts 'Xettings/123'.match(/(settings|)(.*)/)[1];

Resources