-3

I want a regular expression which converts this:

91009-01-28-00 Maximum (c/s)................  1543.5

to this:

91009-01-28-00    1543.5

So basically, a regular expression that escapes alphabets, spaces, forward slashes and brackets.

I have written the following python code so far:

with open('lcstats.txt', 'r') as lcstats_file:
        with open (lcstats_full_path + '_lcstats_full.txt', "a+") as lcstats_full_file:
            lcstats_full_file.write(obsid )
            for line in lcstats_file.readlines():
                if not re.search(r'Maximum [(c/s)]', line):
                    continue
                line = (re.sub(**REGEX**,'',line))  
                lcstats_full_file.write(line)
Neal Titus Thomas
  • 483
  • 1
  • 6
  • 16

4 Answers4

2

It appears you want to have first and last part of the string. If that is the case for every line than spliting it accordingly can be helpful, as in the following code

import re
line = "91009-01-28-00 Maximum (c/s) ................  1543.5"
line=line.split(' ')
line=line[0]+' '+ line[-1]
print(line)

Output:

91009-01-28-00 1543.5
khaldi
  • 472
  • 7
  • 15
1

In your code you are using search to check if you can match Maximum (c/s) and then you want to use a regex to remove that.

I think with your regex Maximum [(c/s)] you mean Maximum \(c/s\). The square brackets make it a character class and (c/s) captures c/s in a capturing group which is not required if you only want to match it.

Wat you could do is match Maximum (c/s) and match one or more times a whitespace or a comma using a character class [ .]+ and replace with an empty string.

Maximum \(c/s\)[ .]+

import re

s = "91009-01-28-00 Maximum (c/s)................  1543.5"
print( re.sub(r"Maximum \(c/s\)[ .]+", "", s))

Demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

Try using this regex /\s[^0-9]+/ This will match from the first space followed by 1 or more not digit characters. You will need to add a space in the replacement string to keep the two bits of remaining data separate.

JGNI
  • 3,933
  • 11
  • 21
0

Regex:

((?<!\d)\D)

Match all non digits\D which is not followed by a digit \d

TheMaster
  • 45,448
  • 6
  • 62
  • 85