解析具有固定长度模式的正则表达式

时间:2021-04-29 21:41:45

Hi… i have a problem to parse a regular expression with fixed length pattern…

嗨...我有一个问题来解析一个固定长度模式的正则表达式...

assuming the following line…

假设以下行......

       __active__         The node where the mouse pointer is currently located.
  1. start with WS until position → 7
  2. 从WS开始直到位置→7

  3. than starting a WORD (everything allowed) sourrounded by → __
  4. 比起___开始WORD(一切允许)

  5. than filled with WS until position → 26
  6. 比填充WS直到位置→26

  7. than a TEXT string is following
  8. 以下是TEXT字符串

i want to have…

我希望有…

  1. the general match → YES or NO
  2. 一般比赛→是或否

  3. if YES → the WORD "active"
  4. 如果是→WORD“活跃”

  5. if YES → the TEXT "The node where the mouse pointer is currently located."
  6. 如果是→TEXT“鼠标指针当前所在的节点”。

update

my core problem is the the 2'nd WS (after __active__) have a variable length depending of the length of __XXXXX…__ (in this example __active__)

我的核心问题是2'nd WS(在__active__之后)具有可变长度,具体取决于__XXXXX的长度... __(在此示例中为__active__)

1 个解决方案

#1


0  

Try this one:

试试这个:

^\s+__(.+?)__\s+(.+)$

The first group, i.e. the characters within the first pair of parentheses capture the work active in your example. The second group will capture the sentence.

第一组,即第一对括号中的字符捕获您示例中的活动工作。第二组将捕获句子。

The regex assumes it is run on a line, and does not care about the positions.

正则表达式假定它在一条线上运行,并不关心位置。

#1


0  

Try this one:

试试这个:

^\s+__(.+?)__\s+(.+)$

The first group, i.e. the characters within the first pair of parentheses capture the work active in your example. The second group will capture the sentence.

第一组,即第一对括号中的字符捕获您示例中的活动工作。第二组将捕获句子。

The regex assumes it is run on a line, and does not care about the positions.

正则表达式假定它在一条线上运行,并不关心位置。