Regex Match without new line ^M

174 views Asked by At

I have a regex in my perl script which is /Operating System:\s*(.*)/. The line I am matching is Operating System: Windows XP Service Pack 3 however, there is a new line after entered after that line. So the text would be seen as Operating System: Windows XP Service Pack 3^M.

Is there anyway to get rid of the ^M using the current regex I have? It captures Windows XP Service Pack 3^Mbut the ^M doesn't display only when I display the content using command prompt or PuTTy's more. It displays when I edit using vi or rather vim.

Any help on this?

2

There are 2 answers

2
vks On BEST ANSWER
Operating System:\s*(.*)(?=\r)

You can try this.

When Searching

...

\n is newline, \r is CR (carriage return = Ctrl-M = ^M)

5
Borodin On

Rather than the look-ahead for CR, I think it's more intuitive to use the pattern that I put in my solution to your main question

/Operating System:\s*(.*\S)/

which captures everything from the first to the last non-space character after Operating System. Apart from being more readable, this pattern's primary advantage is that is portable and doesn't rely on the presence of a trailing CR at the end of the line (which may not be present on the last line of the file anyway).

Both CR and LF count as whitespace, so it will ignore them as well as any trailing spaces or tabs that may be in the file