简体   繁体   English

Java 正则表达式,用于双精度内的任何内容 ** 示例:**文本**

[英]Java regex for anything inside double ** example: **text**

I need the regex for this case scenario:我需要这种情况下的正则表达式:

**This text is bold** this is not **abc123*-#$%&/()** this is not **yes****

So I can have these results separated:所以我可以将这些结果分开:

This text is bold
abc123*-#$%&/()
yes**

I have been struggling with this for hours, help.我已经为此苦苦挣扎了几个小时,求助。

The most close I have been is this:我最接近的是:

\*\*([^\*]\*)\*\*

Actually, I need to use * inside **here(*)** in order to obtain here(*) .实际上,我需要在**here(*)**中使用*才能获得here(*)

I wanna obtain 123?=)* result from **123?=)*** .我想从**123?=)***获得123?=)*结果。

You can use您可以使用

\*\*([^*]*(?:\*(?!\*)[^*]*)*\**)\*\*

See the regex demo .请参阅正则表达式演示

An equivalent is (?s)\*\*(.*?)\*\*(?!\*) , see this regex demo (based on Andreas suggestion ).等效的是(?s)\*\*(.*?)\*\*(?!\*) ,请参阅此正则表达式演示(基于Andreas 建议)。 However, this pattern is about 1/3 slower than the previous one.但是,这种模式比前一种模式慢了大约 1/3。

Details细节

  • \*\* - a ** substring \*\* - 一个** substring
  • ([^*]*(?:\*(?!\*)[^*]*)*\**) - Group 1: any zero or more chars other than * ( [^*]* ) that are followed with zero or more repetitions of a * that is not immediately followed with * (see \*(?!\*) ) and then zero or more non-asterisk chars, and then any amount of asterisks (see the \** at the end of the group pattern) ([^*]*(?:\*(?!\*)[^*]*)*\**) - 第 1 组:除了* ( [^*]* ) 之外的任何零个或多个字符零次或多次重复*不紧跟* (见\*(?!\*) ),然后是零个或多个非星号字符,然后是任意数量的星号(见\**在组模式结束)
  • \*\* - a ** substring \*\* - 一个** substring

The (?s)\*\*(.*?)\*\*(?!\*) is similar, the (?s) is a DOTALL inline modifier that makes a . (?s)\*\*(.*?)\*\*(?!\*)是类似的, (?s)是一个 DOTALL 内联修饰符,它使. match across lines, and then (.*?) matches and captures into Group 1 any zero or more chars as few as possible, up to the first occurrence of ** text that is not immmediately followed with * .跨行匹配,然后(.*?)匹配并尽可能少地将任何零个或多个字符捕获到第 1 组中,直到第一次出现的**文本没有立即跟在*之后。

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

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