Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File Name Parsing
#5
@bowlofred

With regex, at least the PCRE I am used to \. is always a period, and . means 'any character'.
In python it seems r'.' means any character.
Lets do regex strings:
a = "fee...fi....fo.....fum"
b = re.sub("\.","\-",a)
-> fee\-\-\-fi\-\-\-\- (&etc)
and I would need to give a bare "_" for it to work as
-> fee___fi____fo______fum
b = re.sub(r'.',r'_',a)
-> _____________________
The entire string is wiped out.

r'.' is NOT a raw character, at least with the re class.
And escaping '-' (\-)is not working as expected in regex mode.

In Perl I would typically use something like:
x =~ s/^.*(fee).+(fi).+(fo).+(fum).*$/$1,$3,$2,$4/ -> ...fee...fo...fi...fum
I do see that the basic syntax seems OK at:
https://regex101.com/

Though re apparently uses \1 instead of the ancient \$1 format.

I see there is a python-pcre module, but it seems even more obfuscated than a Perl poem.



I started with raw mode, and switched to regex ("foo") mode when I ran into problems.
Not a problem really, all languages have their peculiarities.

By space problem I mean (dot)(space)(backslach). If it write it as is Mybb will kill the space. Cocatenating '.'+'filename' was giving me that space which was screwing up the matching. Omitting the step wound up fixing the problem. os.path apparently subblied its own . for the var.
Reply


Messages In This Thread
File Name Parsing - by millpond - Aug-25-2020, 07:28 AM
RE: File Name Parsing - by ndc85430 - Aug-25-2020, 07:32 AM
RE: File Name Parsing - by millpond - Aug-26-2020, 04:57 AM
RE: File Name Parsing - by bowlofred - Aug-25-2020, 08:41 AM
RE: File Name Parsing - by millpond - Aug-26-2020, 06:26 AM
RE: File Name Parsing - by bowlofred - Aug-26-2020, 08:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Video doing data treatment on a file import-parsing a variable EmBeck87 15 3,135 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,827 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing xml file deletes whitespaces. How to avoid it? Paqqno 0 1,095 Apr-01-2022, 10:20 PM
Last Post: Paqqno
  Parsing a syslog file ebolisa 11 4,388 Oct-10-2021, 05:15 PM
Last Post: snippsat
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,352 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  Error while parsing tables from docx file aditi 1 3,870 Jul-14-2020, 09:24 PM
Last Post: aditi
  help parsing file aslezak 2 2,298 Oct-22-2019, 03:51 PM
Last Post: aslezak
  Python Script for parsing dictionary values from yaml file pawan6782 3 5,048 Sep-04-2019, 07:21 PM
Last Post: pawan6782
  Parsing an MBOX file Oliver 1 8,291 May-26-2019, 07:12 AM
Last Post: heiner55
  parsing complex text file anna 1 2,132 Apr-10-2019, 09:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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