简体   繁体   English

正则表达式从行首删除/ *…* /注释

[英]Regex to remove /*…*/ comments from the start of the line

I have some files of this type: 我有一些这种类型的文件:

/*  78 */     Lorem ipsum dolor sit amet   
/*  79 */     Lorem ipsum dolor sit amet   
/*  eb */     Lorem ipsum dolor sit amet   
/*  HG */     Lorem ipsum dolor sit amet   
/*     */     Lorem ipsum dolor sit amet  
/*  83 */     Lorem ipsum dolor sit amet   
/*  84 */     Lorem ipsum dolor sit amet  
/*     */ 
/*     */     Lorem ipsum dolor sit amet  
/*  ZX */     Lorem ipsum dolor sit amet  
/*     */     Lorem ipsum dolor sit amet  
/*     */     Lorem ipsum dolor sit amet  
/*  90 */     Lorem ipsum dolor sit amet  
/*  91 */     Lorem ipsum dolor sit amet  
/*  92 */     Lorem ipsum dolor sit amet  

And I want to eliminate the 我想消除

/*    */ 
/* 10 */ 

parts of text with regex, my regex looks like: 使用正则表达式的文本部分,我的正则表达式如下:

[/*(0-9)*/]

but it's not working properly, it deletes some texts containing numbers 但无法正常工作,它会删除一些包含数字的文本

Everything inside [ and ] is called a character class , which will always match just a single character. []内部的所有内容都称为一个字符类 ,它将始终仅匹配一个字符。

Inside a character class, the normal regex-meta-chars, like * , ( and ) , loose their special powers. 在字符类内部,普通的正则表达式元字符(例如*()失去其特殊功能。 So [*] matches just the literal '*' . 因此[*]仅匹配文字'*'

In your case, [/*(0-9)*/] will match one of the following chars: '/' , '*' , '(' , ')' , '/' or any (ASCII) digit. 在您的情况下, [/*(0-9)*/]将匹配以下字符之一: '/''*''('')''/'或任何(ASCII)数字。

What you're looking for is the regex: 您正在寻找的是正则表达式:

(?m)^/\*[\s0-9]*\*/

which matches a "/*" followed by zero or more space-chars ( \\s ) or digits ( 0-9 ), ending with "*/" . 匹配"/*"后跟零个或多个空格字符( \\s )或数字( 0-9 ),以"*/"结尾。 The ^ matches the start of the input, and adding a (?m) in front of it makes it match the start of a line. ^与输入的开头匹配,并在其前面添加(?m)使其与行的开头匹配。

So it matches all multi-line comments from your example, except these: 因此,它与您示例中的所有多行注释匹配,但以下情况除外:

/*  eb */
/*  HG */
/*  ZX */

Try this expression 试试这个表情

/^\/\*[0-9\s]*\*\//

Or this expression if you want also to delete /* AH */ 如果您还想删除/* AH */则使用此表达式

/^\/\*[\w\s]*\*\//

请尝试此操作,它使开始和结束/ *和* /与中间的一些文本匹配。

/^\/\*.*?\*\//

尝试使用以下正则表达式:

/\/\*.*?\*\//

Try this regular expression 试试这个正则表达式

\/\*.*?\*\/\s+

\\/\\* matches the starting / and * \\/\\*匹配开始的/*

.*? matches any amount of text without being greedy 匹配任意数量的文本而不会贪婪

\\*\\/ matches the ending * and / \\*\\/匹配结尾*/

\\s+ matches any amount of white space characters (optional) \\s+匹配任意数量的空格字符(可选)

If you're on Linux (or have access to it), you can do without regexes: 如果您使用的是Linux(或可以使用它),则可以不使用正则表达式:

cut -b 10- <INPUTFILE>

Or if the strint is fixed width and on the beginngin of the line, with eg sed : 或者,如果strint是固定宽度并且在行的开头,例如sed

sed '/^.\{10\}//' <INPUTFILE>

Or with awk (if there are only two / on a line: 或使用awk (如果一行上只有两个/

awk '{ print gensub(".*/","",1,$0) }'

HTH 高温超导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM