1

So a line in the text file looks like this:

......-...-..--- (hello)

I want to find all lines that only have dots on the even positions. So I think I need to repeat searching for the regex pattern .\. which allows any character on the first position and requires a dot as the second one.

I don't know, however, how to keep looking for this pattern until a space followed by parentheses is found.

The following output would be correct:

....-. (word)
-.-.-.. (word)
-.-.....-.-. (word)

1 Answer 1

0

Instead of looking for any character on odd positions, simply only look for the legal characters (your dit, \. and your dah, -, I guess?). The regex way of expressing "any of this set of characters" is […], i.e., your elementary pattern becomes [\.-]\..

By the way, without spaces between the letters in your morse code, it's impossible to unambigously interpret the morse code. (Morse is not a prefix-free code; encoded letters can be the found at the beginning of other letters.) So, I think your examples are kind of broken; you will need to insert spaces.

1
  • Thanks for your answer. what I'm trying to do is find the lines which only contain dits/dots on even places. However, I don't know how to write an regex that looks for a repeated pattern. I basically need a regex that looks for the pattern of "either dot or dash followed by a dot" repeated throughout the line.
    – Tomyy
    Oct 20, 2022 at 16:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .