-1

Let's say:

s = "this is a sentence. My GPA is 1.10. Thanks to StackOverflow, otherwise I would have a lower one."

I want the following:

new_s = "this is a sentence my gpa is 1.10 Thanks to StackOverflow otherwise I would have a lower one"

This is what I do:

new_s = s.lower()
new_s = new_s.replace(',', '')

where I struggle is with replacing the point that goes in 1.10

if I do:

new_s = new_s.replace(',', '')

I won't get the desired output. is there a way to select only the desired points (not used to depict decimal numbers)?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
lalaland
  • 379
  • 3
  • 15

1 Answers1

1

Use re.sub() with a negative lookahead in the regular expression.

import re

new_s = re.sub(r'\.(?!\d)', '', s)

This matches a . that isn't followed by a digit.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thank you, sir. I don't fully understand it, but I will try to see what's the mechanism. :) – lalaland Sep 30 '20 at 19:11
  • 1
    That's why I included a link to a tutorial. – Barmar Sep 30 '20 at 19:12
  • I know, I am reading it. :) Thank you – lalaland Sep 30 '20 at 19:13
  • 1
    @lalaland regular expression is a standalone language for string processing. – Ekrem Dinçel Sep 30 '20 at 19:14
  • 2
    here is a [regex101 link](https://regex101.com/r/rE4vqy/1) that shows it, and on the right it gives a explanation of the regex. you can try and play around with it :D – vivax Sep 30 '20 at 19:15
  • @Kowalski in the past I always tried to stay away from RegEx. – lalaland Sep 30 '20 at 19:16
  • @lalaland you can still stay away from regex, and sometimes you should. You can solve your current problem by using a little state machine, but using regex is not very terrible. – Ekrem Dinçel Sep 30 '20 at 19:24
  • @Kowalski If regular expressions are too advanced, writing their own state machine will not be easy. – Barmar Sep 30 '20 at 19:27
  • 1
    @Barmar I don't agree. OP already knows Python and writing a little state machine in Python is not that hard. But regex is an another language to learn, which is not the best solution always. – Ekrem Dinçel Sep 30 '20 at 19:30
  • Thanks for your interest! learning, in general, is a good thing. Easy or not I will have to deal with it – lalaland Sep 30 '20 at 19:39
  • just one more thing : if now I want to replace the \n but still want to preserve the \\n that comes from the \\nu for example in the LATEX code, how can I use RegEx to select the desired \n knowing that some \n are merges to words (i.e. hi\nbye). I tried to implement what they said in the tutorial : ``` r"\\n(?!\\)" ``` but I don't think it works because that selects it. I would need some kind of NOT (what I have) – lalaland Sep 30 '20 at 19:41
  • I'm not sure what you're trying to do there? Are you trying to remove newlines? They don't have actual backslashes in them, those are just in string literals. So you don't need to do anything to avoid matching Latex escape sequences. – Barmar Sep 30 '20 at 19:47