1

Have a below output and wanted to remove the white space and line breaks.

\n', '    var [3:0] apple [1:0];\n', '    int mango;\n', '    float banana [5:0];\n', '    int lichi;\n', '  

I tried both the below code but its not working. Any comments/corrections ?

print str(str(re.sub('[^A-Za-z0-9[:]]+[\r\n]+', ' ', str(match_group_t[i]))))
print re.sub(r"(?<=[a-z])\r?\n"," ", match_group_t[i]) 

Drop in your comments for any correction !

Expected Output is : Only var [3:0] apple [1:0], int mango, float banana [5:0], int lichi. So that I can store type and name separately in an array and use it later. Like

variable data in an array: [apple, var, lelem, [3:0], rleme, [1:0]]
6
  • 1
    Whats your expected output? Commented Aug 18, 2018 at 23:04
  • You are not gonna want to use regex for removing whitespace and linebreaks in python. Just do string.replace(" ", "") and string.replace("\n", "") Commented Aug 18, 2018 at 23:05
  • I'm not totally clear on what you want your output to be (e.g., do you want to preserve the space in var apple?), but I don't see why a variant of print(re.sub("\s{2,}", "", s)) wouldn't suffice offhand. Commented Aug 18, 2018 at 23:09
  • @truecam: print match_group_t[i].replace("\n",""). Is not replace the required \n with "". Commented Aug 18, 2018 at 23:38
  • This is what I got. It worked for me >>> a = "\n asifjdiasd fjj jads \n" >>> a.replace("\n", "") ' asifjdiasd fjj jads ' Commented Aug 18, 2018 at 23:44

2 Answers 2

3

You can use the expression in re.sub:

(?:[;\n']|\s{2,})
  • (?: Non capturing group
    • [;\n'] Characters ; , \n and '.
    • | Or
    • \s{2,} Whitespace, two or more.
  • ) Close non capturing group.

Python code:

import re
mystr = "\n', '    var [3:0] apple [1:0];\n', '    int mango;\n', '    float banana [5:0];\n', '    int lichi;\n', '  "

print(re.sub(r"(?:[;\n']|\s{2,})",r'',mystr)[2:])

Prints the desired output:

var [3:0] apple [1:0], int mango, float banana [5:0], int lichi, 
Sign up to request clarification or add additional context in comments.

7 Comments

I tried your tried above, But this is the output I am getting. Output before applying your approach: \n', ' var [3:0] apple [1:0];\n', ' int mango;\n', ' float banana;\n', ' int lichi;\n', ' Output after applying your approach: , var [3:0] apple [1:0]\n, int mango\n, float banana\n, int lichi\n, Can you please provide some note on your approach so that I can try to edit and I am slightly new to this reg approach.
How did you try it? Copy and paste it in and should print what I showed in my answer. I'll add notes for the regex.
Standalone it works, when I tried using in the bigger program its printing the different output. Bigger problem is posted in below link for different query: stackoverflow.com/questions/51905869/…
Bigger program do you mean a bigger text file?
@UnbearabelLightness: Have tried your approach in the bigger code and updated the code.. But output is slightly different in there. stackoverflow.com/questions/51905869/…
|
2

Consider using the built-in isspace instead of regular expressions.

newstring = ''.join(ch for ch in oldstring if not ch.isspace())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.