如何在regex中表示双引号字符(“)? [英] How to represent the double quotes character (") in regex?

查看:650
本文介绍了如何在regex中表示双引号字符(“)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用regex来检查字符串是否以双引号字符开头(),并以双引号字符。



问题是我不能使用双引号字符,还有任何其他方式在regex中表示引号字符,或者在字符串中表示

  String s =; //?


解决方案

首先,双引号字符在regex中没有什么特别之处 - 它只是另一个字符,所以它不需要从正则表达式的角度转义 / p>

但是,因为java使用双引号来定义字符串常量,如果要在java中使用双引号创建一个字符串,必须避开它们。



此代码将测试您的String是否匹配:

  if(str.matches(\。* \)){
//此字符串以双引号开头和结尾
}

注意,您不需要添加输入标记的开始和结束( ^ $ ),因为 matches()要求整个输入匹配返回true - ^ $


I need to use regex, to check if a string starts with a double quotes character (") and ends with a double quotes character too.

The problem is I can't use a double quotes character, cause it gets confused. Is there any other way to represent a double quotes character " in regex, or in string in general?

String s = """;    // ?

解决方案

Firstly, double quote character is nothing special in regex - it's just another character, so it doesn't need escaping from the perspective of regex.

However, because java uses double quotes to delimit String constants, if you want to create a string in java with a double quote in it, you must escape them.

This code will test if your String matches:

if (str.matches("\".*\"")) {
    // this string starts and end with a double quote
}

Note that you don't need to add start and end of input markers (^ and $) in the regex, because matches() requires that the whole input be matched to return true - ^ and $ are implied.

这篇关于如何在regex中表示双引号字符(“)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆