LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2012, 03:30 AM   #1
blueskynet
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Rep: Reputation: Disabled
Need help with regular expression, /[a-z][\.\?!]\s+[A-Z]/


Hi there,
This is my first time here so I hope I don't post this thread in a wrong place.
Can someone help me with this regular expression /[a-z][\.\?!]\s+[A-Z]/ ? I don't really know how this part,[\.\?!] work.

Thank you.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-29-2012, 03:36 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi and welcome to LQ!

Certain characters are special and need to be escaped when you want the literal meaning.

A . (dot) tells a regular expression to look for any character, \. on the other hand means a literal . The ? is also special (zero or one), \? means a literal ?

[\.\?!] => look for a dot or a question mark or an exclamation point. (see post #4)

Also have a look here: Regular-Expressions.info

Hope this helps.

Last edited by druuna; 04-29-2012 at 04:41 AM. Reason: Oeps.....
 
1 members found this post helpful.
Old 04-29-2012, 04:21 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
I would add that it is the user being over cautious to as when in a character list, [], they lose there special meaning :0
 
Old 04-29-2012, 04:35 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
More than just being over-cautious, it actually adds "\" to the characters in the bracket list. Depending on the input, it could even change your results.

I'd like to know what the purpose is for this regex anyway. It appears to be designed to match sentence breaks; lowercase letter, followed by punctuation, spaces, and a capital letter. But since the match also consumes the letters and the punctuation, the application of it would have to be treated with care.
 
1 members found this post helpful.
Old 04-29-2012, 04:40 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,839

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
Whoa - all the heavy-hitters.

Time for me to exit stage-left ....
 
Old 04-29-2012, 04:55 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Some sed examples to point out the difference.

Code:
$ echo 'a. B  c? D  e! F  g\ H' | sed -r 's/[a-z][.?!]\s+[A-Z]/---/g'
---  ---  ---  g\ H

$ echo 'a. B  c? D  e! F  g\ H' | sed -r 's/[a-z][\.\?!]\s+[A-Z]/---/g'
---  ---  ---  ---
If you want to preserve any part of the match for the output, you have to use backreferences.

Code:
$ echo 'a. B  c? D  e! F  g\ H' | sed -r 's/([a-z])[.?!]\s+([A-Z])/\1---\2/g'
a---B  c---D  e---F  g\ H

$ echo 'a. B  c? D  e! F  g\ H' | sed -r 's/([a-z])[\.\?!]\s+([A-Z])/\1---\2/g'
a---B  c---D  e---F  g---H
 
2 members found this post helpful.
Old 04-29-2012, 04:58 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by syg00 View Post
Whoa - all the heavy-hitters.

Time for me to exit stage-left ....
...says the person with by far the highest post count in the thread.
 
Old 04-29-2012, 05:46 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Yeah I'm with David ... I feel we are still the juniors (maybe not by age?)
 
Old 04-29-2012, 06:54 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,839

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
lol - post count is such a mindlessly inane metric.
I keep telling jeremy to delete it all together. Post quality is what elevates all the (other) responders to this (and other) thread.

Quality, not quantity. I've learnt heaps from all of you.
 
Old 04-29-2012, 02:41 PM   #10
blueskynet
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you very much. This is actually one of my homework question. The info you guys shared are very helpful. I will keep try out to learn more about this 'ugly' stuff
Best to all!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression for example.* prayag_pjs Linux - Newbie 3 02-17-2012 05:40 PM
Regular expression help smeezekitty Programming 7 03-27-2011 08:57 PM
Regular Expression 0.o Programming 3 06-09-2009 03:28 AM
Regular expression datbenik Programming 1 01-05-2006 02:58 PM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 11:08 AM


All times are GMT -5. The time now is 06:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration