gukwon
gukwon

Reputation: 85

How to extract dot(.) ended sentence?

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.

Upvotes: 0

Views: 1307

Answers (3)

Poul Bak
Poul Bak

Reputation: 10930

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.

Upvotes: 1

Nikolas
Nikolas

Reputation: 44398

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).

.+[^\.]\.$

Upvotes: 1

Micha Wiedenmann
Micha Wiedenmann

Reputation: 20843

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

Upvotes: 1

Related Questions