How to create a regex with lookbehind statement that still works at the beginning of a line

I need to emulate the behavior of \b at the beginning of a line, where I add extra characters to the set, which is considered the boundary of the word. Right now I'm using something like:

 "(?<=\\W|\\p{InCJKUnifiedIdeographs})foo" 

This works the way I would like it to, unless I start the string being matched: in this case, the statement fails and I don't get hit. What I want is equivalent to a match if I am at the beginning of a line, or foo preceded by a character without a word or ideograph. But I can’t get the right spell to support it.

Any thoughts? Or is it impossible?

Thanks in advance.

+4
source share
1 answer
 "(?<=^|\\W|\\p{InCJKUnifiedIdeographs})foo" 

Just add a line beginning binding to the lookbehind conditions.

+12
source

Source: https://habr.com/ru/post/1335133/


All Articles