LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2010, 01:02 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Regular Expression Question


How do I write a regexp that matches a string that begins with whitespace or contains a newline, but not necessarily both?
 
Old 12-10-2010, 01:34 PM   #2
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
By "contains a newline" do you mean consists only of a newline, or has a newline in it at all? If it's the former then
Code:
^\s
should do.
 
Old 12-10-2010, 01:38 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your question involves a bit of gray area. Firstly, regexes as used with most tools are line oriented, and as such do not contain embedded newlines, as these are record separators. Secondly, newlines in most contexts are considered to be whitespace.
In Perl and Awk, record separators can be modified from their defaults, so a record may contain an embedded newline. After all that, Perl makes a special case out of records that contain newlines; see http://www.regular-expressions.info/dot.html for the gory details.
Having said all of that, it often doesn't make sense to try to match too much or cover too many circumstances in a single regex. This may be just such a case, where you might be better off using a couple of regexes matches, and couple them with logical operators:
Code:
# perl example:
   if( $stringData =~ m/^\s+/ || $stringData =~ m/\n/s ){
       print "Yup, it matches\n";
   }
Bottom line, is 'it depends' on the specific environment.

--- rod.
 
  


Reply



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 Question yuye811 Programming 7 06-19-2009 01:55 AM
Regular Expression Question windisch Programming 8 05-22-2007 03:27 PM
Regular expression question gauge73 Linux - General 2 10-28-2005 09:32 AM
regular expression question zero79 Linux - Software 1 07-11-2005 07:03 PM
Regular expression question - John Sloan Linux - Software 1 09-08-2004 12:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:18 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
Open Source Consulting | Domain Registration