Python Forum
Facing issue in python regex newline match
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Facing issue in python regex newline match
#1
Hi Everyone,

.Myc PGGGGG_UIII_Q9999_AB A0 B11 B22 D1 D2 D3
+ 4I 3B

I am trying to match the above two line using regex.

\.(myc|Myc)\s+([a-zA-Z0-9\_\-]+)\s+(.*)\n(.*) = Used this regex

It will print --> .Myc PGGGGG_UIII_Q9999_AB A0 B11 B22 D1 D2 D3

It is not going to newline(\n is NOT WORKING HERE). Please let me know how to go to newline in python regex.
Suggestions are appreciated.

Thank you!
Reply
#2
Please post your code. I don't know what re command you are using or what arguments are passed other than the pattern. Are you using the MULTILINE flag?
Reply
#3
[quote="deanhystad" pid='173623' dateline='1697137364']
Please post your code. I don't know what re command you are using or what arguments are passed other than the pattern. Are you using the MULTILINE flag?
I am using re.compile:

re.compile(r''\s*^\.(myc|Myc)\s+([a-zA-Z0-9\_\-]+)\s+(.*)\n(.*)") -- But after lot of research I got to know that the raw string(r") used here won't consider \n as new line, it treats \n as \n itself. But I don't know how to remove raw string here.

And I tried using re.M, re.DOTALL and it didn't work, please let me know what needs to be done.
Reply
#4
How to match new line in python regex. ( if \n DOES'NT WORK)
buran write Oct-13-2023, 10:34 AM:
Don't create new threads unnecessarily. I merged the new thread into old one.
Reply
#5
https://stackoverflow.com/questions/1468...raw-string
Reply
#6
Added r.
import re

input_string =\
""".Myc PGGGGG_UIII_Q9999_AB A0 B11 B22 D1 D2 D3
+ 4I 3B"""

pattern = r'\.(myc|Myc)\s+([a-zA-Z0-9\_\-]+)\s+(.*)\n(.*)'
matches = re.findall(pattern, input_string)[0]
print(matches)
Output:
('Myc', 'PGGGGG_UIII_Q9999_AB', 'A0 B11 B22 D1 D2 D3', '+ 4I 3B') >>> matches[0] 'Myc' >>> matches[-1] '+ 4I 3B'
Reply
#7
Thanks everyone! Thanks a lot @snippsat
I will try and update the result.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 456 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  issue in using regex akbarza 4 591 Nov-14-2023, 10:00 AM
Last Post: buran
  Failing regex, space before and after the "match" tester_V 6 1,204 Mar-06-2023, 03:03 PM
Last Post: deanhystad
  Regex pattern match WJSwan 2 1,288 Feb-07-2023, 04:52 AM
Last Post: WJSwan
  Python Regex quest 2 2,366 Sep-22-2022, 03:15 AM
Last Post: quest
  Facing problem with Pycharm - Not getting the expected output amortal03 1 867 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  Match substring using regex Pavel_47 6 1,463 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  CSV to Text File and write a line in newline atomxkai 4 2,717 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  Facing Problem while opening a file through command prompt vlearner 4 1,928 Jan-30-2022, 08:10 AM
Last Post: snippsat
  Match key-value json,Regex saam 5 5,442 Dec-07-2021, 03:06 PM
Last Post: saam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020