function - writeLines

Hi! I am learning regular expressions in recent times. When I use writeLines() function, I got a unexpected result. I think writeLines("12\bd") will output 12d, but it output 1d in reality. The digit in front of \b seems to be hidden. Could you help me explain this result?
image

I don't think writeLines() uses regular expressions at all. Regular expressions are used to match text and writeLines() is simple writing text. I don't know what writeLines() is doing with the \b combination, but understanding it will not help you use regular expressions.
You might find working with a function like sub() more useful.

sub(pattern = "12\\b", "X", "there are 12 fish")
[1] "there are X fish"
> sub(pattern = "12\\b", "X", "there are 123 fish")
[1] "there are 123 fish"

Notice I use a double back slash. I think that is necessary because both R and the regular expression engine interpret the \ character.

OK, thanks. I just konw \b actually rewind the cursor one bit.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.