How do we write an exception in a Regex.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do we write an exception in a Regex.
# 1  
Old 08-03-2012
How do we write an exception in a Regex.

Hello,
Actually this is a follow-up of my earlier request to identify Sentence Boundaries while generating snippets for a search engine. The basic regex I have written to delimit sentence boundaries handles numbers and acronyms but I cannot get it to handle cases of
Quote:
Mr. Andrew visited me.
Mrs. Smith left for London.
The full stops after Mr. Mrs. are automatically treated as sentence delimiters which is not desirable.
I tried the following syntax:
Code:
!(Dr\.|Mr\.|Mrs\.|Ms\.|[A-Z]\.|i\.e\.|w\.r\.t\.|e\.g\.|etc\.|viz\.)

to make the regex ignore a full-stop after such cases enumerated, but it does not work.
In fact the simple regex I had written has got murky and just does not perform any more.
Any help in correcting the regex would be appreciated.

Some sample sentences are given below:
Quote:
Mr. Andrew came.
Ms. Smith left for London.
He brought three things viz. bread, cheese and wine
This is w.r.t. your application
# 2  
Old 08-03-2012
Instead of writing things for a regex to not match, try getting something else in your regex to match it first. Regexes do greedy matching so whatever matches it first 'wins'.

What language is this regex for? This works in grep:

Code:
$ echo "Mr. Andrew visited me.  fleeb narf stuff." | egrep -o "([a-zA-Z]|(Mr|Ms|Dr|Mrs)[.]| )*[.]"
Mr. Andrew visited me.
  fleeb narf stuff.

$

A simplified example but hopefully conveys the idea.

Just a preference of mine, but I find it clearer to put special chars in [] than escape them to make them literal sometimes.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-03-2012
Many thanks. Works beautifully in egrep, but dies in Java. I wonder why. Does anybody know if Java demands a special regex set ?
# 4  
Old 08-04-2012
regex really isn't the same everywhere. Might have been a good idea to post you were using java from the start.
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

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Monitor logs for exception and if exception come then sent an email

Hi Folks, please advise , I have logs generated on unix machine at location /ops/opt/aaa/bvg.log , now sometimes there come exception in these logs also, so I want to write such a script such that it should continuously monitor these logs and whenever any exception comes that is it try to find... (3 Replies)
Discussion started by: tuntun27272727
3 Replies

4. Shell Programming and Scripting

Regex to split a string and write the output in another file.

hi, i am trying to write a script to generate ouput in the following format: ##### buildappi abcd_sh nodebug.##### ##### buildappi ijk_sh nodebug.##### The given string is as follows: xtopSharedDLLs = "abcd_sh def_sh ijk_sh " \ + "jkl_sh any_sh... (15 Replies)
Discussion started by: Rashid Khan
15 Replies

5. Shell Programming and Scripting

Regex : help - How to write a range in ls

Experts, Quick question for you guys: - There are a lot of files. - How to list all files in one command from arch1_171034 to 63 , in the below examples. That means how to list with ls : arch1_171034_667780.dbf to arch1_171063_667780.dbf files. Thanks . (7 Replies)
Discussion started by: rveri
7 Replies

6. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Shell Programming and Scripting

Exception handling

Sometimes when I try to use curl to upload to an ftp server, I get the message: $curl -T file.wmv ftp.eu.filesonic.com --user user:password curl: (8) Got a 421 ftp-server response when 220 was expected How do I get the script to try again if I get the message curl: (8)? (2 Replies)
Discussion started by: locoroco
2 Replies

8. 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

9. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

10. Shell Programming and Scripting

Exception Handling

Hi, I have written a script to load csv files into a mysql database, however, i would like for the shell script to exit in the event of an error (missing file, load error etc.) - currently if an error is encountered the next statement is processed - This is how i am loading the csv scripts ... (5 Replies)
Discussion started by: bertpereira
5 Replies
Login or Register to Ask a Question