String regex comparisons


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String regex comparisons
# 1  
Old 11-18-2014
String regex comparisons

Here is the sample code:
Code:
str1="abccccc"
str2="abc?"
if [[ $str1 =~ $str2 ]]; then
  echo "same string"
else
  echo "different string"
fi

Given that ? implies 0 or 1 match of preceding character, I was expecting the output to be "different string", but I am seeing "same string".

Am I not using the regex comparisons in right way?

thanks,
RCK
# 2  
Old 11-18-2014
try it with quotes around your variables;
Code:
if [[ "$str1" =~ "$str2" ]]; then

# 3  
Old 11-19-2014
ongoto: No. Quoting $str2 in this context performs a string comparison instead of an ERE match.

Rameshck: The ERE abc? will match ab or abc appearing anywhere in the expansion of $str1. To match only ab or abc you need to anchor the ERE on both ends:
Code:
str1="abccccc"
str2='^abc?$'
if [[ $str1 =~ $str2 ]]; then
  echo "same string"
else
  echo "different string"
fi

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 11-19-2014
@ Don Cragun
Aha!
I find that ...
Quote:
the behaviour of the =~ (regex) operator has changed since Bash 3.2
Good catch! Thanks.
This User Gave Thanks to ongoto For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep with regex containing one string but not the other

Hi to you all, I'm just struggling with a regex problem and I'm pretty sure that I'm missing sth obvious... :confused: I need a regex to feed my grep in order to find lines that contain one string but not the other. Here's the data example: 2015-04-08 19:04:55,926|xxxxxxxxxx| ... (11 Replies)
Discussion started by: stresing
11 Replies

2. Shell Programming and Scripting

String comparisons

Can someone please tell me what is wrong with this stings comparison? #!/bin/sh #set -xv set -u VAR=$(ping -c 5 -w 10 google.com | grep icmp_req=5 | awk '{print $6}') echo I like cookies echo $VAR if "$VAR" == 'icmp_req=5' then echo You Rock else echo You Stink fiThis is the error.... (6 Replies)
Discussion started by: cokedude
6 Replies

3. Shell Programming and Scripting

Nawk - print only a string containing a regex.

Hi again, I'm looking for some help with nawk, I can print a line which has a regex match in it from a file using /pattern/ but I'm looking for a way to only print the $tring which contains the pattern, rather than the whole line. This $tring may be of variable length, may occur at any point... (1 Reply)
Discussion started by: spynappels
1 Replies

4. Shell Programming and Scripting

Perl: Regex, string matching

Hi, I've a logfile which i need to parse and get the logs depending upon the user input. here, i'm providing an option to enter the string which can be matched with the log entries. e.g. one of the logfile entry reads like this - $str = " mpgw(BLUESOAPFramework):... (6 Replies)
Discussion started by: butterfly20
6 Replies

5. Shell Programming and Scripting

filtering out duplicate substrings, regex string from a string

My input contains a single word lines. From each line data.txt prjtestBlaBlatestBlaBla prjthisBlaBlathisBlaBla prjthatBlaBladpthatBlaBla prjgoodBlaBladpgoodBlaBla prjgood1BlaBla123dpgood1BlaBla123 Desired output --> data_out.txt prjtestBlaBla prjthisBlaBla... (8 Replies)
Discussion started by: kchinnam
8 Replies

6. Shell Programming and Scripting

Bash string replacement - how to use regex?

Hello I have a bash script where I need to do a substring replacement like this: variable2=${variable1/foo/bar} However, I only want "foo" replaced if it is at the end of the line. However, this does not work: variable2=${variable1/foo$/bar} as you can see I'm using the $ regex for... (2 Replies)
Discussion started by: Ubuntu-UK
2 Replies

7. Shell Programming and Scripting

using regex to get part of a string ?

Hi there, i wonder, is it possible to use regular expressions to partially select a string? I have a bunch of server names which look like this server1z-test server2z2 server45z-primary server13z3 I want to extract up to and including the 'z' in the server name, so for example ... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

8. Shell Programming and Scripting

Perl Regex string opperation

I'm working on a basic log parser in perl. Input file looks like: len: 120713 foo bar file size of: testdir1/testdir1/testdir1/testdir1/testfile0 is 120713Of course there are tens of thousands of lines... I'm trying to compare the len and filesize values. #!/usr/bin/perl use strict; use... (2 Replies)
Discussion started by: dkozel
2 Replies

9. UNIX for Dummies Questions & Answers

regex on first string in a variable.

Hi all, this is driving me nuts. I need to evaluate if a variable in a shell script has a heading s or m character e.g. s92342394 or m9233489 if so then I need to get rid of them. I'm quite familiar with PERL and could do it there in 3 mins but I have not found a decent way to do this in a shell.... (1 Reply)
Discussion started by: Endo
1 Replies
Login or Register to Ask a Question