Search code examples
c#regexregular-language

Regex: Find words with multiple periods


I am given a task that will require regex to find a string out of a paragraph. I need to find a string that looks something.like.this but is not limited to looking.something.like.this.also.

Using the paragraph above as an example, this expression would have pulled out "something.like.this" and "looking.something.like.this.also"

Must me A-z, no =+[]{}\/?*&^%$#@!*, can include .()-

I thought about checking to see if there are multiple periods between spaces.


Solution

  • Is this regex what you're looking for?

    (?:[A-Za-z(\)-]+(?:\.[A-Za-z\(\)-]+)+){2,}
    

    Additional criterias in comments

    • Cannot match ... .

      This would also match ... which is not included in the OP's requirements. – juharr

      @Zsw i didnt think about it, but @juharr is correct, i cannot have ... – Bernie

    • Cannot match consecutive periods.

      What about consecutive periods like this..has...multiple....periods - juharr

      @juharr i should also ignore this, but it will be highly unlikely that will appear in any of the text it will run against. – Bernie

    • Cannot match one.two.

      @Zsw one.two cannot be matched. I found using [A-Za-z.()-]+.[A-Za-z.()-]+.[A-Za-z.()-]+ works for this, but how do i ignore the ... or this...this..this? – Bernie