What is the difference between single line mode and multiline mode in Regular expressions?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users What is the difference between single line mode and multiline mode in Regular expressions?
# 1  
Old 06-30-2011
Bug What is the difference between single line mode and multiline mode in Regular expressions?

Hi All,

Can please let me know what is the difference between the single line mode and multi line mode in regular expresions?

Thanks,
Chidhambaram B
# 2  
Old 07-01-2011
If the stuff you need to see is on more than one line, you need to bring the multiple lines into the purview of one regex, like using N in sed to add lines to the buffer. There are additional regex for this situation, like '\n', and commands, like sed P.

Google says Regex Tutorial - Start and End of String or Line Anchors

Using ^ and $ as Start of Line and End of Line Anchors
If you have a string consisting of multiple lines, like first line\nsecond line (where \n indicates a line break), it is often desirable to work with lines, rather than the entire string. Therefore, all the regex engines discussed in this tutorial have the option to expand the meaning of both anchors. ^ can then match at the start of the string (before the f in the above string), as well as after each line break (between \n and s). Likewise, $ will still match at the end of the string (after the last e), and also before every line break (between e and \n).
In text editors like EditPad Pro or GNU Emacs, and regex tools like PowerGREP, the caret and dollar always match at the start and end of each line. This makes sense because those applications are designed to work with entire files, rather than short strings.
In all programming languages and libraries discussed on this website , except Ruby, you have to explicitly activate this extended functionality. It is traditionally called "multi-line mode". In Perl, you do this by adding an m after the regex code, like this: m/^regex$/m;. In .NET, the anchors match before and after newlines when you specify RegexOptions.Multiline, such as in Regex.Match("string", "regex", RegexOptions.Multiline).
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 07-02-2011
Bug Thanks for the response.

Thanks very much for your response.

Can you please clear my question below?

I tried the below perl one-line commands but I could not get feel the result as expected.

echo "One 1\nTwo 2\nThree 3" | perl -ne 'print if m/^Two (\d+)$/m'

echo "One 1\nTwo 2\nThree 3" | perl -ne 'print if /(?m)^Two (\d+)$/'

But I got a result as expected when I executed the .pl file the same. Please find the file below.

#!/usr/bin/perl

$inputstring="One 1\nTwo 2\nThree 3";
if ( $inputstring =~ /^Two (\d+)$/m )
{
print "Matching\n";
}

Can you please tell why the perl one-line command has not given the output?
# 4  
Old 07-02-2011
$ perldoc perlrun

Quote:
Code:
       -n   causes Perl to assume the following loop around your program,
            which makes it iterate over filename arguments somewhat like sed
            -n or awk:

              LINE:
                while (<>) {
                    ...             # your program goes here
                }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

From a C++ application how to find if a hpux host is in standard mode or trusted mode

is there a way for my C++ application to find out which mode the hpux OS is running in? standard mode or trusted mode. (3 Replies)
Discussion started by: einsteinBrain
3 Replies

2. Solaris

Single user mode

Dear All I am trying to install my os as : ok>boot cdrom - install but receiving the following : "IDprom checksum error getexecname() failed /sbin/rcS /etc/vfstab cannot create INIT:failed write utmpx enrty INIT:single user mode INIT:execle of /etc/sulogin failed Enter run level" Can you... (6 Replies)
Discussion started by: hadimotamedi
6 Replies

3. Solaris

Single user mode

Hi all I am new on sun OS. I have have little experience on linux. The Story start from this point: I want to put some script on start-up the terminal, but I cant do that. my shell was sh and I tried so much to find way to do that. at last someone said to me change your shell to bash. I ask how... (4 Replies)
Discussion started by: Rahim_T
4 Replies

4. Solaris

DNS service is in maintenance mode. How to bring it back to online mode?

:confused: when i tried to look the status of DNS-client, it is in maintenance mode..... Please tell me how to bring it back to online mode...PLEASE TELL ME STEP BY STEP.... PLEASE... :wall: (2 Replies)
Discussion started by: vamshigvk475
2 Replies

5. SuSE

Convet Linux OS from text mode to graphic mode

Hi All, I used to have my suse linux(VM) server in graphic mode but not anymore since morning. I cant rolback since i loose somuch work. Any idea how to it back to normal. Thanks (6 Replies)
Discussion started by: s_linux
6 Replies

6. UNIX for Dummies Questions & Answers

how to Single user mode?

How to enter single user mode when UNIX/LINUX system is starting? (1 Reply)
Discussion started by: gkreddy
1 Replies

7. AIX

Boot in Single-Mode

Hi All, I have AIX 5.1 & I forgot the root password. I find out the solution is to boot in single-mode and remove the root password from the /etc/passwd file. My question is how to boot in single-mode? Also is there any password required when booting in this mode? (9 Replies)
Discussion started by: aldowsary
9 Replies

8. UNIX for Advanced & Expert Users

single user mode

Is there another way of switching to single user mode except by typing /usr/sbin/shutdown 0 ??? :rolleyes: (5 Replies)
Discussion started by: kekanap
5 Replies

9. UNIX for Dummies Questions & Answers

Single user mode

Hi all, Well back at work and back to crashing systems again :-) Does anyone know where I can find some decent information on single user mode? I need to be able to fix a few things. Don't know if it's possible in single user mode but I need to fix the "etc/vfstab" mainly I re-wrote it to... (2 Replies)
Discussion started by: merlin
2 Replies

10. UNIX for Dummies Questions & Answers

single user mode

Hi all, why "vi" acts differently is single user mode? Does anyone help ? I am using "x" to delete and it keeps messing up. Please help Thanks (2 Replies)
Discussion started by: guest100
2 Replies
Login or Register to Ask a Question