Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java为问号开头的句子编写正则表达式_Java_Regex - Fatal编程技术网

如何使用java为问号开头的句子编写正则表达式

如何使用java为问号开头的句子编写正则表达式,java,regex,Java,Regex,下面是我的字符串 The course are: ? This is sample php program ? This is sample java program 如何编写用于删除的正则表达式?在句末加上pullstop(.) 所需输出: The course are: This is sample php program. This is sample java program. 如何做到这一点,请建议我。 谢谢为什么要用正则表达式呢?你真的不需要正则表达式。你可以试试这样的东西

下面是我的字符串

The course are:
? This is sample php program
? This is sample java program
如何编写用于删除的正则表达式?在句末加上pullstop(.)

所需输出:

The course are:
This is sample php program.
This is sample java program.
如何做到这一点,请建议我。
谢谢

为什么要用正则表达式呢?你真的不需要正则表达式。你可以试试这样的东西

   String str = "? This is sample php program";
    if(str.startsWith("?")){
        String newStr = str.substring(str.indexOf("?")+1, str.length()).trim() + ".";
        System.out.println(newStr);  
    } else{
        System.out.println(str);
    }

您可以在不使用正则表达式的情况下执行此操作:

String str = "? This is sample php program";
Pattern p = Pattern.compile("\\? ");
Matcher m = p.matcher(str);
if(m.find())
{   
    str = m.replaceFirst("");
    str = str+".";
}
System.out.println(str);

我认为它可以替换为single
String#replaceAll(String regex,String replacement)
call:

String repl = s.replaceAll("\\?\\s*(.*?)(?=\\n|$)", "$1.");
现场演示: 更新:替换
或项目符号:

String repl = s.replaceAll("[?•]\\s*(.*?)(?=\\n|$)", "$1.");

到目前为止,您已经尝试了什么?您并不需要正则表达式。我正在使用这个正则表达式\\s*([\\d\\w]\)\[\\d\\w]\.\.\.\124;?)\\ s*但是它的给出错误。为什么在其中一个句子的结尾出现了句号?好的,那么为什么在最后一个示例句子的结尾没有出现句号?这个字符串发生了什么
"? 你好吗?“我很好”
它将替换所有的
是的,它将删除?并在句子结尾处添加句号。@Ruchira:谢谢你的建议,修改了答案。如果字符串不是以“?”开头,它也会添加句号。如果字符串包含“你好吗?”?“我很好”则o/p将是“我很好”。@Ankullathi感谢您指出我的问题。请查看我的编辑answer@RuchiraOP说我的要求是,如果句子是stats with?我需要删除(?)并在句子末尾加上句号。但在你的情况下,每次都是“附加”。“那么其他的就不是了needed@MayurB参见此
Required output:课程是:这是一个php示例程序。这是示例java程序
,感谢它的运行。此外,我们还有包含项目符号的文本•这是php示例程序•这是linux程序手册如何执行此操作,请建议我。是否要将项目符号也替换为结尾处的点?检查编辑后的答案。