How to compare strings using =~? [closed] - regex

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I'm trying to create a string that will verify if input time is in the right format. I keep getting the 'else' portion to execute, but never the 'then' portion. I'm not sure where in the string there is a mistake. I execute the script in the shell using ./. I test it with 01:20. It will give me "Time entered is valid." when I input single digit int values. I want it to recognize the 00:00 format. Any suggestions?
echo "enter time" ; read time
if [[ '^(([01][0-3])|([2][0-9]))[:][0-5][0-9]$' =~ $time ]]
then
echo "Time entered is valid."
else
echo "Time entered is NOT correct."
fi

=~ is not commutative; the string you want to match must go on the left, the regular expression on the right.
if [[ $time =~ '^(([01][0-3])|([2][0-9]))[:][0-5][0-9]$' ]]

Related

How to use if else comparing string in a batch file? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I would like to check the string is match or not. I tried this way, but it always return error, syntax error, I don't know which syntax that error.
Error message
The syntax of the command is incorrect.
if TXT EQU TXT(
SET Format=TXT
REM ECHO %Format%
if %Format% EQU TXT(
ECHO Format correct
GOTO END
)
ECHO Format not correct
This works for me:
#echo off
set format=TXT
if "%format%"=="TXT" (
#echo Format correct
goto :end
)
#echo Format not correct
:end

How to check the format of keyboard input for a Perl script? [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 8 years ago.
Improve this question
My Perl script just takes whatever you give as an input, and I want make it more robust by checking the pattern of the input string. My input string has to be in the format xxxxx-xxxx-xxxx. How can I check that?
$foo =~ /^.{5}-.{4}-.{4}\z/s
For example, this will repeatedly ask for the value until it gets a valid one.
my $foo;
while (1) {
print("Please provide foo (xxxxx-xxxx-xxxx): ");
my $foo = <STDIN>;
die("EOF\n") if !defined($foo);
chomp($foo);
last if $foo =~ /^.{5}-.{4}-.{4}\z/s;
print("Invalid input\n");
}

Syntax error near unexpected token 'newline' while reading data from file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a problem with my programme. I try to read data from the file like this:
./programme <input.txt>
but there is an error:
bash: syntax error near unexpected token 'newline'
in my programme I use getline for reading data:
while(getline(cin,string))
{
...
}
It works properly when I input data from keybord.
This is what contains file input.txt:
0.2 0.1
1.2 0.2
1.2 1.1
1 2 0
0 1 2
You have to actually write to the standard input.
Try one of these:
./programme < input.txt
On Unix:
cat input.txt | ./programme
On Windows:
type input.txt | programme
I suppose that you actually literally typed:
./programme <input.txt>
That's a bash syntax error, because > must be followed by the name of a file to which output will be directed. But there is no file, so bash complains that it unexpectedly found the end of the line.
Similarly, < is followed by the name of a file from which input is taken.
So you presumably meant to write:
./programme < input.txt
Or you could have written
./programme < input.txt > output.txt
which would take input from input.txt and send output to output.txt.
(Whitespace before and after < and > is optional.)

Bash: cutting a delimited fragment of each string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file containing lines that look like this:
GTTCAGAGTTCTACAGTCCGACGATCGGATGAGNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCTCCGAGTNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCCTTATATNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCGAAGTGCNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCAAGTTTTNNNNNN
GTTCAGAGTTCTACAGTCCGACGATCCGACGAANNNNNN
I want to remove the first 26 and final 6 characters from each line. I haven't been able to write a good regular expression to accomplish that using vi, but I'm not sure what else to do.
Any suggestions?
Thanks!
Try with grep.
This will keep the last 13 characters and then the first 7, returning only the matching characters (-o) with the Perl-compliant -P flag:
grep -oP ".{13}$" foo.txt | grep -oP ".{7}"
If your file name is foo you can use cut to grab out the range of chars you want:
$ cut -c27-33 foo
This produces:
GGATGAG
TCCGAGT
CTTATAT
GAAGTGC
AAGTTTT
CGACGAA
cut can take a character range, if the lines are a fixed size (they appear to each be 39 characters)
cut -c27-33 file.txt

how to replace ’ with ’ in perl [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 9 years ago.
Improve this question
I have below piece of code in Perl
my $file2 = "devil’s-claw-extract.html";
$file2 =~ s/’/’/ig;
print "$file2";
This code is working fine(means replacing ’ with ’) when running in konsole but not working in browser.
Please help me out.
Of course it “works”, and I will believe so until you produce a self-contained example that indicates otherwise.
Your first problem is that you are reinventing the wheel, there already is a module on CPAN to do such escaping for you:
use utf8; # because this source file contains special chars
use HTML::Entities;
my $file2 = "devil’s-claw-extract.html";
print encode_entities $file2;
Output:
devil’s-claw-extract.html
or with encode_entities_numeric:
use utf8;
use HTML::Entities 'encode_entities_numeric';
my $file2 = "devil’s-claw-extract.html";
print encode_entities_numeric $file2;
Output:
devil’s-claw-extract.html
Secondly, it is worth noting that your input string contains ’, a single right quote. This has the codepoint U+2019 (not U+0092, which is in a private use area. Conveniently, it decodes to ’ in the Windows-1252-encoding, but the actual encoding should always be explicitly set).
The apostrophe ', which you likely wanted, is U+0027 or &apos;.