Invalid character range error in a new function [duplicate]

我的未来我决定 提交于 2019-12-29 08:08:29

问题


I have a problem with the follwoing error message

invalid regular expression '([a-Z]*)_(.*)', reason 'Invalid character range'

so the line of code which causes the error is

if(tide=="long") names(problem) <- sub("([a-Z]*)_(.*)","\\2",problem)

so if long is selected for the parameter tide in the function the names of problem shall be defined ....

but when I enter function(...,tide="long",..) the above mentioned error message is displayed.


回答1:


You can't use [a-Z], because the letters are in the wrong order, but anyway it is better to use:

[a-zA-Z]

The problem is that those ranges are based on tables (either ASCII or Unicode), but the uppercase letter "Z" comes before the lowercase letter "a" so the range is in the wrong order.

The other solution to use [A-z] would be a valid range, but there are the characters

[\]^_`

between the letter "Z" and the letter "a", so this range would include characters that you normally don't want to match.




回答2:


Your problem is this [a-Z]

You have to write either : [a-z] or [a-zA-Z]



来源:https://stackoverflow.com/questions/11966975/invalid-character-range-error-in-a-new-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!