1234
1234

Reputation: 579

Anyone know how to use Text.Regex for word boundary?

I use Text.Regex package right now.

I try to use subRegex for word boundary,

let r = mkRegex "\\bdog\\b"

subRegex r "mydog dog" "(\\0)"

output => "mydog dog"

I also try following:

let r = mkRegex "\\b(dog)\\b"

subRegex r "mydog dog" "(\\0)"

output => "mydog dog"

I try to change "mydog dog" => "mydog (dog)"

Upvotes: 1

Views: 78

Answers (1)

rampion
rampion

Reputation: 89093

Text.Regex from the regex-compat package uses POSIX-compatible regular expressions, not PCRE ones.

In POSIX regular expressions, the left word boundary is \< and the right word boundary is \>:

ghci> let r = mkRegex "\\<dog\\>"
ghci> subRegex r "mydog dog" "(\\0)"
"mydog (dog)"

Upvotes: 1

Related Questions