0

I need to extract sentence ends with dot '.', but don't extract sentence ends in ' ...' (blank and three dots).

Example:

I love you.
I love you too ...

I want to match first sentence not second.

i image python style pseudo code:

for string in strings:
    checker1 = (string == .)
    if checker:
        checekr2 = (prev_string(string) != blank)
        if checker2:
            extract_all_strings()
        else:
            pass
    else:
        pass

but I can't image regular expression code.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
gukwon
  • 85
  • 5

3 Answers3

1

You can use (?<! \.\.)\.$, see the demo.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
  • I have to extract all sentence when pattern is matched. your solution is only extract dot(.) but your answer give me insight of regex. thank you!! – gukwon Nov 26 '18 at 15:19
1

You can use the following regex:

[\w ]+\.(?!\.)

It matches one or more either Word character or Space, then it use a neagative look ahead to make sure there's only one dot.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
  • thank you for your kind answer. it's really fantastic solution in my case. in fact, I should consider this pattern surrounded by other sentences. your solution is perfect!! – gukwon Nov 26 '18 at 15:15
1

Here you go with a very simple Regex:

[\w ]+\.$

Test the solution on Regex101.

  • [\w ] is a group of allowed characters, where \w stands for any character from [a-zA-Z0-9_] and stands for space itself.
  • [\w ]+ where + means that there the characters from the group described in the point above might appear between one and unlimited times.
  • \. is the dot itself which must be escaped, otherwise the dot . matches any character.
  • $ stands for the end of a string.

This together assures that only a sentence ending with exactly one dot will be caught.

Another and less strict approach might be allow anything, where the 2nd character from the end is not a dot and the last one is a dot (Regex101).

.+[^\.]\.$
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • thank you for your detail answer. your answer has given me a better understanding of regular expression. really appreciate – gukwon Nov 26 '18 at 15:17