0

I am trying to solve following task:

Match the pattern abc.def.ghi.jkl, where each variable a,b,c,d,e,f,g,h,i,j,k,l can be any single character except the newline.

For above question I am matching the input to regex :

 "([^\\n]{3}(.)){3}([^\\n]{3})"
 // this is the regex pattern I am using currently

What am I doing wrong? Please help me correct the above regex so that it does not match the incorrect input I have provided in the title. Currently it matches to it somehow. Although I have provided 3 it is apparently matching to more than 3 characters.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
vibhor vaish
  • 153
  • 1
  • 13
  • 1
    You need to also quote the `.` otherwise it matches any character. – Thilo Nov 20 '16 at 09:13
  • 1
    What are you trying to achieve via `(.)`? Are you sure that this is correct way of doing it? What makes you think so? – Pshemo Nov 20 '16 at 09:14
  • But i haven't used \\ characters in front of . therefore it should work as a simple dot , shouldn't it ? – vibhor vaish Nov 20 '16 at 09:15
  • 3
    @vibhorvaish: *"therefore it should work as a simple dot , shouldn't it "* No, an unescaped `.` in a regular expression means "any character here." – T.J. Crowder Nov 20 '16 at 09:16
  • At least related: http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean – T.J. Crowder Nov 20 '16 at 09:16
  • @Pshemo can you unmark it ? – vibhor vaish Jul 02 '19 at 04:07
  • Please **don't** edit/change already answered question into entirely new one. Instead use ["Ask question"](https://stackoverflow.com/questions/ask) button and ask your [*new/separate*](https://stackoverflow.com/revisions/74f5c212-dd93-4efa-bb54-ce6d300be1f1/view-source) question. – Pshemo Jul 02 '19 at 06:15
  • @Pshemo I am unable to ask new questions due to negative reputation for this one. Is this how stackoverflow aims to help us ? At the time of asking this question, I didn't know that the above issue was due to \\. character , that's why I had asked new one and then it was marked as duplicate. Please suggest what to do here . – vibhor vaish Jul 02 '19 at 09:07
  • "I am unable to ask new questions due to negative reputation for *this* one" no, having one question with -1 vote is will not ban worthy. You most likely asked more questions which received worse score and now are deleted. Those are *also* included in score which counts toward question-ban. Anyway removing already answered question by changing it into new one defeats the purpose of this site which is gathering programming questions and answers so that *all* of us facing those problems could get help, not just asker. – Pshemo Jul 02 '19 at 10:20
  • What to do next is discussed at [Before you post your next question](https://meta.stackoverflow.com/q/254262), [What can I do when getting “We are no longer accepting questions/answers from this account”?](https://meta.stackoverflow.com/q/255583). – Pshemo Jul 02 '19 at 10:21
  • I don't think this question is bad per se. Yes, you could avoid asking simple questions like these by using something like [an online regex tester](https://www.freeformatter.com/java-regex-tester.html), but saying this question already has an answer in the _"regex meta dot vs literal dot"_ topic feels unfair ; if they knew the answer to the question was the dot thing, they wouldn't have asked the question. I'm grateful to the people who helped me with my bad questions back when I was a beginner. That said, I'm no expert with the SO guidelines, so I might be wrong here. – adentinger Jun 17 '21 at 14:45

1 Answers1

4

. has a special meaning in regular expression patterns.

If you want to get a "simple dot", you need to quote/escape it (as "\\.").

And that special meaning is (under normal configuration) "any character except line breaks", which exactly matches your other condition, so you can simplify this to

"(...)\\.(...)\\.(...)\\.(...)"
Thilo
  • 257,207
  • 101
  • 511
  • 656