check expression with minimum 3 character and not start with Space [duplicate] - expression

This question already has answers here:
Regex: How to match a string that is not only numbers
(11 answers)
Closed 3 years ago.
My code is currently using the following Regular expression which matches on numbers:
/^([^0-9]*)$/
it's working fine when I'm putting Character and Special Character like
hello
merry#
mom&dad
but when I'm trying with a number it's not giving me a proper result
hello 22
mom&dad44
mm88$
I want output like this
1234 - Not Allow
mnb5 - Not Allow
123Hello - Not Allow
Hello123 - Allow
hello - Allow
Hello% - Allow
Hello%123 - Allow
hello 123 - Allow
Hello # - Allow
I want Minimum 3 characters and it does not start with Space and number

Just use this regex without anchors:
/\D/
This will match a non-digit i.e. \D anywhere in a line.
RegEx Demo

Related

Git bash on Windows different result than terminal on CentOS for regex [duplicate]

This question already has answers here:
Removal of special characters from string using perl script
(2 answers)
Closed 9 months ago.
See the following cleanCustomer.sh file
#!/bin/bash
customer=Reportçós
cleanedCustomer=${customer//[^a-zA-Z0-9 \-_.]/}
echo $cleanedCustomer
When I run it on Windows 11 in Git Bash it prints Reports.
When I run it on CentOS in terminal it prints Reportçós.
Anybody knows why is a-z interpreted as alpha characters in CentOS and not in Windows?
How do I ensure only english characters are considered in the CentOS?
From the bash manual:
A pair of characters separated by a hyphen denotes a range expression; any character that falls between those two characters, inclusive, using the current locale’s collating sequence and character set, is matched. If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched. A ‘-’ may be matched by including it as the first or last character in the set.
Your Git Bash locale uses rules that don't match accented characters in ranges like a-z, your CentOS locale does. This can be addressed by using a consistent locale like C for collation. Plus your - is in the wrong spot; it needs to be first or last, and the backslash needs to be escaped with another backslash to match a literal one.
#!/bin/bash
LC_COLLATE=C
customer=Reportçós
cleanedCustomer=${customer//[^a-zA-Z0-9 \\_.-]/}
printf "%s\n" "$cleanedCustomer"

How to remove | substract directory name from bash? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
Within my bash script, which is running on a Linux server, I have a variable that points to a specific path, let's call this my-path.
my-path="/home/vMX-ENV/vMX-21.1R1/"
echo "The path is: $my-path"
...
From the given variable my-path, I am looking for a way only to display the version number 21.1R1.
The following is an example of what I am trying to accomplish.
./script.sh
the path is: /home/vMX-ENV/vMX-21.1R1/
the version is: 21.1R1
Is there a way to do this?
Thanks!
Bash has a fairly wide variety of built-in mechanisms for manipulating variables' values. Of particular interest for the present problem are parameter expansion forms that remove prefixes or suffixes that match specified shell patterns. For example:
# Remove any trailing slash and store the result in DIRNAME_NORM
DIRNAME_NORM=${DIRNAME_MAIN%/}
# Emit the value of $DIRNAME_NORM, less the longest prefix matching shell
# pattern *vMX-
echo "${DIRNAME_NORM##*vMX-}"
There is no need to rely on an external program for this case.
Using sed grouping and back referencing
$ sed 's/[^0-9]*\([^/]*\).*/\1/' input_file
21.1R1
/[^0-9]* - Exclude anything up to the next occurance of a digit character. As this part is not within the parenthesis () to be grouped, it will be excluded.
\([^/]*\) - This will group everything from the first digit up to the next occurance of / slash.
.*/ - Exclude everything else
\1 - Return the group with backreference \1.
awk can also be used.
$ awk -F"[/-]" '{print $6}' input_file
21.1R1
-F"[/-]" - Set delimiter to / and - then print column 6 which will contain the string.

Extract a substring from the "-" sign until the string´s end [duplicate]

This question already has answers here:
In VBScript how do you take a string using mid and split the string at say a ";"
(2 answers)
Closed 4 years ago.
I´m looking for any way to remove substring from string starting in the "-" symbol until the string´s end.
Server Down - Windows Server BAWFM-055
Server Up - Linux Server LSWFM-089
In the first example the output will be "Windows Server BAWFM-055"
In the second example the output will be "Linux Server LSWFM-089"
Trim(Mid(MyString, InStr(MyString, "-") + 1))
Where MyString is a variable holding your strings.
The +1 is needed to Skip the minus sign and start at the next character.
Trim to remove eventual leading or trailing spaces.

Split a string by two delimiters [duplicate]

This question already has answers here:
Split string by multiple delimiters
(6 answers)
Closed 6 years ago.
I want to split a string by whitespaces and # using a single ruby command.
word.split(" ") will split by whitespaces ;
word.split("#") will split by '.
How to do all three at once?
Use regular expressions' character class to do that:
word.split(/[ #]/)
To match any whitespace character use \s : word.split(/[\s#]/)
A character class is delimited with square brackets ([, ]) and lists
characters that may appear at that point in the match. /[ab]/ means a
or b, as opposed to /ab/ which means a followed by b.
/\s/ - A whitespace character: /[ \t\r\n\f]/

replacing all numbers by a single word in text files [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Is there a easy and efficient one line solution to replace all the numbers or sequences that contain numbers and symbols (\ / $ & * # # ) ( - + ! ~ . , : ; " ' ` ^ % _ ] [ { } = ), for example:
1 2 3 4 998898321321
0.2 1.2 32221.111. 1321321321.111
111.11212.21212
212323/12331/321312
121-12123-32131
121+12123+32131
1_212121_2320
12131!~~~323131
etc
with a single token NUMBER in a huge text (100GB) file? Sample input and output:
input:
hello my friend 212323/12331/321312
hope you are fine 12131!~~~323131 in 33-years from now
happy face is important to maintaion by 98987 321321/32131
output:
hello my friend NUMBER
hope you are fine NUMBER in 33-years from now
happy face is important to maintaion by NUMBER NUMBER
Basically anything between two space that contains numbers and non-alphabetic symbols must be replaced by NUMBER. The rest of the text should be kept as-is.
Okay, I think I got this:
I need three steps:
Double up the white spaces
Replace all non-letter characters surrounded by a space or newline with NUMBER (while retaining the spaces)
Collapse double white spaces into single ones
This is how it looks now:
$ cat test.txt
hello my friend 212323/12331/321312
hope you are fine 12131!~~~323131 in 33-years from now
happy face is important to maintaion by 98987 321321/32131
123 This is a line
$ sed -r 's/ / /g;s/(^| )[^[:alpha:] ]+( |$)/\1NUMBER\2/g;s/ / /g' test.txt
hello my friend NUMBER
hope you are fine NUMBER in 33-years from now
happy face is important to maintaion by NUMBER NUMBER
NUMBER This is a line
To complement chw21's helpful solution with a perl solution that can handle not just spaces, but any mix of spaces and tabs between words:
perl -ple 's/(^|(?<=[[:blank:]]))[^[:alpha:][:blank:]]+((?=[[:blank:]])|$)/NUMBER/g' file
The use of look-behind ((?<=...) and look-ahead ((?=...)) assertions obviates the need for capture groups, and therefore the need for doubling spaces as an intermediate step; using [[:blank:]] (space or tab) in lieu of (just space) makes it work with any mix of spaces and tabs:
(^|(?<=[[:blank:]])) matches the beginning of a line (^) or any character preceded by a blank (space or tab)
[^[:alpha:][:blank:]]+ matches any nonempty run of characters composed of only non-letters and non-blanks
((?=[[:blank:]])|$) matches at the end of the line ($) or if the following character is a blank.

Resources