0

I'm want search newline character in string using regex in python.I don't want to include \r or \n in Message.

I have tried regex which is able to detect \r\n correctly. But when i'm removing \r\n from Line variable. still it prints the error.

Line="got less no of bytes than requested\r\n"

if(re.search('\\r|\\n',Line)):
      print("Do not use \\r\\n in MSG");

It Should detect \r\n in Line variable which is as a text not the invisible \n.

It should not print when the Line is Like below:

Line="got less no of bytes than requested"
  • 2
    Use `Line.strip()`. Gives `'got less no of bytes than requested'` as output. No need to use regex then. – Arkistarvh Kltzuonstev Mar 28 '19 at 13:10
  • no, use `line.rstrip()` to remove just on the right, not the spaces on the left – Jean-François Fabre Mar 28 '19 at 13:15
  • what do you want to achieve? detect end of line or remove end of line? – Jean-François Fabre Mar 28 '19 at 13:15
  • It makes sense to use raw strings for regex so you don't need to escape escape characters `r'\r|\n'` – fabianegli Mar 28 '19 at 13:19
  • no need to use raw string for \r or \n. it works without – Jean-François Fabre Mar 28 '19 at 13:25
  • @Jean-FrançoisFabre well, yes and no. If you match only `\r` or `\n` individually (as indicated in the code), you are right but if you match `\r\n` (as indicated in the text) it matters. However, this inconsistency between text and code makes answering this question a gamble. Also, the `Line` variable holds actual line breaks not the visible characters `\r\n` as indicated in the Text. Finally, knowing about raw strings might help the OP. – fabianegli Mar 28 '19 at 17:58

5 Answers5

0

Instead of checking for newlines, it would probably be better to just remove them. No need to use regex for it, just use strip, it will remove all whitespace and newlines from the ends of the string:

line = 'got less no of bytes than requested\r\n'
line = line.strip()
# line = 'got less no of bytes than requested'

If you want to do it with regex you can use:

import re

line = 'got less no of bytes than requested\r\n'
line = re.sub(r'\n|\r', '', line)
# line = 'got less no of bytes than requested'

 

If you insist on checking for the newlines, you can do it like this:

if '\n' in line or '\r' in line:
    print(r'Do not use \r\n in MSG');

Or the same with regex:

import re

if re.search(r'\n|\r', line):
    print(r'Do not use \r\n in MSG');

Also: it's advisable to have your Python variables named with snake_case.

ruohola
  • 21,987
  • 6
  • 62
  • 97
0

You are looking for the re.sub function.

Try to do this:

Import re
Line="got less no of bytes than requested\r\n"
replaced = re.sub('\n','',Line)
replaced = re.sub('\r','',Line)
print replaced 
  • It will DO the action, but question author wants to achieve CHECK (without doing anything); It is good to suggest using different approaches, but only as addition to your answer. Otherwise you actually are not answering the question – maxwell Mar 28 '19 at 13:32
0

If you just want to check for line breaks in the message, you can use the string function find(). Note the use of raw text as indicated by the r in front of strings. This removes the need to escape the backslash.

line = r"got less no of bytes than requested\r\n"
print(line)
if line.find(r'\r\n') > 0:
    print("Do not use line breaks in MSG");
fabianegli
  • 2,056
  • 1
  • 18
  • 35
0

First of all consider to use strip as many guys here mentioned.

Second, if you want to match newline at ANY position in string use search not match

What is the difference between re.search and re.match?Here is more about search vs match

newline_regexp = re.compile("\n|\r")
newline_regexp.search(Line)  # will give u search object or None if not found
maxwell
  • 857
  • 8
  • 16
-1

As others have noted, you are probably looking for line.strip(). But, in case you still want to practice regex, you would use the following code:

Line="got less no of bytes than requested\r\n"

# \r\n located anywhere in the string
prog = re.compile(r'\r\n')
# \r or \n located anywhere in the string
prog = re.compile(r'(\r|\n)')


if prog.search(Line):
    print('Do not use \\r\\n in MSG');
ritlew
  • 1,622
  • 11
  • 13
  • because you are wrong in so many cases, 1st to match start and end of the string in regexp there are special characters — ^ and $. 2nd, you should use `search` not `match` if you want to check anywhere in the string – maxwell Mar 28 '19 at 13:29
  • @maxwell are you happy now that I did it your way? – ritlew Mar 28 '19 at 13:36
  • You are redefining same `prog` variable → it will match only second regexp – maxwell Mar 28 '19 at 13:38
  • Yep. They are examples with comments explaining which each do. One should be chosen based on what is needed. – ritlew Mar 28 '19 at 13:39
  • for authors question the only second is applicable and also you are using regepx groups `()` which are not needed in this case – maxwell Mar 28 '19 at 13:41
  • It isn't obvious which the author wanted because his regex was the second version, while the output string reflected the first. – ritlew Mar 28 '19 at 13:43
  • This is not as i want. I want if string explicitly has \r\n character than only it should print message as Do not use \r\n. – PRUTHVIBEN PATEL Mar 28 '19 at 15:57
  • So the first prog variable is what you want? – ritlew Mar 28 '19 at 15:59