Regex not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex not working
# 1  
Old 10-11-2013
Wrench Regex not working

I am using a regex to exactly match a string abcdef as
Code:
^abcdef$

. But it does'nt seem to work Smilie
# 2  
Old 10-11-2013
In your case
Code:
^abcdef

that is enough.
The start AND end definitions are only required if you pass the start AND the end of a the string to search, meaning an incomplete one or one holding further variables.

That would be done by:
Code:
^abc.xyz$

Matching each line starting with abc AND ending with xyz ;
regardless if there is the alphabet in between or not.

Hope this helps
# 3  
Old 10-11-2013
What input do you have ?
What output do you expect ?
Please provide a representative sample so people may then be able to help you to achieve what you want.
# 4  
Old 10-11-2013
In what computer language? bash / ksh93 shell?
Code:
str=abcdef
if [[ $str =~ ^abcdef$ ]]; then
  echo match
fi

# 5  
Old 10-11-2013
I tried the
Code:
^abcdef

first but then it matched other strings like
Code:
abcdefghi

. So I wanted to match the exact string.

I tried to put in
Code:
ab.ghi

. Still not working.Smilie Please help.
# 6  
Old 10-11-2013
did you try with the dollar sign at the end ?

Code:
^abcdef$

If you states it's not working, could you please show us exactly how you test the matching ?
Code:
$ cat toto
gfedcba
abcdefg
abcdef
abcdefg
Abcdef
fgqjghjk
rueaioyu
$ grep ^abcdef$ toto
abcdef
$


Last edited by ctsgnb; 10-11-2013 at 04:54 PM..
# 7  
Old 10-11-2013
Weird thing is, it didnt work at first..
But now does..

Code:
+ ~ $ echo abcdefg|grep ^abcdefg
abcdefg
:) ~ $ echo abcdefg|grep ^abcdefg$
abcdefg
 ~ $ echo abcdefg|grep ^abc.efg$
abcdefg
 ~ $ echo abcdefg|grep ^abc*fg$
:( ~ $ echo abcdefg|grep ^abc.fg$
- ~ $ echo abcdefg|grep ^abc.efg$
abcdefg
 ~ $ echo abcdefg|grep ^abc?efg$
- ~ $

hth
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

How to make working this regex in perl?

Hello to all, The Regex below is supposed to match all strings except RR45. I've tested in regex101.com and it works, butwhen I try to use it with the perl command below I get the error shown. Regex=(?<=^|RR45)(?!RR45).+?(?=RR45|$) How to fix this? I'm using Cygwin. $ echo... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

3. Shell Programming and Scripting

If statement with [[ ]] and regex not working as expected

Using BASH: $ if -- ::00" ]]; then echo "true"; else echo "false"; fi false Mike (5 Replies)
Discussion started by: Michael Stora
5 Replies

4. UNIX for Dummies Questions & Answers

Gsub regex not working

I have a number of files that I pass through awk/gsub. I believe to have found a working regex and on 'test bed' sites it matches, however within gsub it does not. Examples: Initial data: /Volumes/Daniel/Public/Drop Box/_Hellsing_Ultimate_OVA_-_10_.mkv gsub & regex: gsub("\]+\]","" ... (4 Replies)
Discussion started by: unknownn
4 Replies

5. Shell Programming and Scripting

need to grep contents of a file within specific time span. regex i am using is not working

Hi , I am trying to extract contents of a file between specified time stamp. but it does not seem to work. i am trying to extract output of /var/adm/messages between 15:00:00 to 15:23:59 . i have tried two regex the first one seems to kind of work. it displays some output. the second one is... (13 Replies)
Discussion started by: chidori
13 Replies

6. Shell Programming and Scripting

matching a regex using egrep not working

Hi, I'm trying to validate if a string matches a regular expression, but it is not working. Am I missing something? Do I need to scape any of the characters? if echo 'en-GB' | egrep '({1,8})(-{1,8})*' >/dev/null; then echo Valid value fi Thanks in advance (6 Replies)
Discussion started by: skrtxao
6 Replies

7. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

8. Shell Programming and Scripting

bash with: if, elif & regex not working

Why is only hello3 being printed? There must be some kind of syntax problem because the file list definitely includes all the file extensions line by line. #!/bin/bash find '/home/myuser/folder/' -name '*.c' -type f | while read F do if ] # if the file name ends in .txt.c then ... (6 Replies)
Discussion started by: cyler
6 Replies

9. Shell Programming and Scripting

BASH regex (convert from working perl version)

Hi there, I need to test that a variable ($VAR) matches a regex mask in BASH. I have the exact thing working in perl (below), but could somebody advise me how i would do the same in BASH ? do i need to use something like egrep ? #!/bin/perl -w my $VAR = "some value"; if ( $VAR =~... (4 Replies)
Discussion started by: rethink
4 Replies

10. Shell Programming and Scripting

gnu sed regex grouping not working?

Hello, from the gnu sed manual, I should be able to do this: `\(REGEXP\)' Groups the inner REGEXP as a whole, this is used to: * Apply postfix operators, like `\(abcd\)*': this will search for zero or more whole sequences of `abcd', while `abcd*' ... (3 Replies)
Discussion started by: Allasso
3 Replies
Login or Register to Ask a Question