相关疑难解决方法(0)

正则表达式匹配C风格的多行评论

我有一个字符串,例如

String src = "How are things today /* this is comment *\*/ and is your code  /*\* this is another comment */ working?"
Run Code Online (Sandbox Code Playgroud)

我想从字符串中删除/* this is comment *\*//** this is another comment */src串.

我尝试使用正则表达式但由于经验不足而失败.

java regex string

18
推荐指数
2
解决办法
2万
查看次数

preg_replace出CSS评论?

我正在编写一个快速preg_replace来从CSS中删除注释.CSS注释通常具有以下语法:

/* Development Classes*/
/* Un-comment me for easy testing
  (will make it simpler to see errors) */
Run Code Online (Sandbox Code Playgroud)

所以我试图杀死/*和*/之间的所有内容,如下所示:

$pattern = "#/\*[^(\*/)]*\*/#";
$replace = "";
$v = preg_replace($pattern, $replace, $v);
Run Code Online (Sandbox Code Playgroud)

没有骰子!它似乎在正斜杠上窒息,因为如果我将/ s取出模式,我可以删除注释文本.我尝试了一些更简单的模式,看看我是否可以丢失斜杠,但它们会保持原始字符串不变:

$pattern = "#/#";
$pattern = "/\//";
Run Code Online (Sandbox Code Playgroud)

关于为什么我似乎无法匹配那些斜杠的任何想法?谢谢!

php regex preg-replace

6
推荐指数
3
解决办法
6180
查看次数

标签 统计

regex ×2

java ×1

php ×1

preg-replace ×1

string ×1