java regexp match morse code

自闭症网瘾萝莉.ら 提交于 2021-01-27 10:58:30

问题


I'm trying to test if the Morse code is legal.

public static boolean isMorseCode( String code ){
return code.trim().matches("[+.|+\s|+-]");      
}

The Morse code line can begin with a . (known as Dit) and a - (known as Dah). It can have spaces in the line. 1 space between two specific codes like .- -. in this case the sum of codes which are only separated by one white space is a word. But it can also have 3 white spaces and then a new word begins. I'm using trim to exclude the fact that there could be a white space at the beginning and end of the line.

Here an example to make it more clear.

We use "Hello World" for explanation. The white spaces are wiped out by posting them so take a look at http://www.rubular.com/r/r2iwqgUHCB there is also the regexp I used. In java it doesn't work.If someone could explain why it doesn't work or even show me how it does work I would be very thankful.


回答1:


You can describe the morse code with this:

[.-]{1,5}(?> [.-]{1,5})*(?>   [.-]{1,5}(?> [.-]{1,5})*)*

each elements of the Morse alphabet have between 1 or 5 Dit or Dah.

each letters are separated by one space

each words are separated by three spaces




回答2:


It may be because [+.|+\s|+-] is a character class, so it may just be matching a single character. Maybe what you want is [.\s\-]+.




回答3:


Out of curiosity why not just validate the string contains the known sequences?

This regex will match only match if the string contains valid Morse code letters, punctuation, or numbers. Any white space at the beginning or end of the string is just ignored automatically. And the expression requires either 1 or 3 spaces between characters.

^\s*(?:\s*(?:\.-|-\.\.\.|-\.-\.|-\.\.|\.|\.\.-\.|--\.|\.\.\.\.|\.\.|\.---|-\.-|\.-\.\.|--|-\.|---|\.--\.|--\.-|\.-\.|\.\.\.|-|\.\.-|\.\.\.-|\.--|-\.\.-|-\.--|--\.\.|-----|\.----|\.\.---|\.\.\.--|\.\.\.\.-|\.\.\.\.\.|-\.\.\.\.|--\.\.\.|---\.\.|----\.|\.-\.-\.-|--\.\.--|\.\.--\.\.|\.----\.|-\.-\.--|-\.\.-\.|-\.--\.|-\.--\.-|\.-\.\.\.|---\.\.\.|-\.-\.-\.|-\.\.\.-|\.-\.-\.|-\.\.\.\.-|\.\.--\.-|\.-\.\.-\.|\.\.\.-\.\.-|\.--\.-\.)(?=\s|\s{3}|\s*$))+\s*$

Java code example

Code

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
  public static void main(String[] asd){
  String sourcestring = "         .... . .-.. .-.. ---   .-- --- .-. .-.. -..      ";
  Pattern re = Pattern.compile("^\\s*((?:\\s*?(?:\\.-|-\\.\\.\\.|-\\.-\\.|-\\.\\.|\\.|\\.\\.-\\.|--\\.|\\.\\.\\.\\.|\\.\\.|\\.---|-\\.-|\\.-\\.\\.|--|-\\.|---|\\.--\\.|--\\.-|\\.-\\.|\\.\\.\\.|-|\\.\\.-|\\.\\.\\.-|\\.--|-\\.\\.-|-\\.--|--\\.\\.|-----|\\.----|\\.\\.---|\\.\\.\\.--|\\.\\.\\.\\.-|\\.\\.\\.\\.\\.|-\\.\\.\\.\\.|--\\.\\.\\.|---\\.\\.|----\\.|\\.-\\.-\\.-|--\\.\\.--|\\.\\.--\\.\\.|\\.----\\.|-\\.-\\.--|-\\.\\.-\\.|-\\.--\\.|-\\.--\\.-|\\.-\\.\\.\\.|---\\.\\.\\.|-\\.-\\.-\\.|-\\.\\.\\.-|\\.-\\.-\\.|-\\.\\.\\.\\.-|\\.\\.--\\.-|\\.-\\.\\.-\\.|\\.\\.\\.-\\.\\.-|\\.--\\.-\\.)(?=\\s|\\s{3}|\\s*$))+)\\s*$",Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  Matcher m = re.matcher(sourcestring);
    if(m.find()){
      for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
        System.out.println( "[" + groupIdx + "] = " + m.group(groupIdx));
      }
    }
  }
}

Output

Capture group 0 gets the entire matching input string. Group 1 gets the trimmed sentence.

[0] =>          .... . .-.. .-.. ---   .-- --- .-. .-.. -..      
[1] => .... . .-.. .-.. ---   .-- --- .-. .-.. -..

The regex will return nothing (aka false) if the string is not valid.



来源:https://stackoverflow.com/questions/17197887/java-regexp-match-morse-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!