0

When using Special Character in Regex, I faced some problem. In some special characters, ', ",[] the \\(special character) is not working. When I logged case by case, the special character was problem. Is there any one who know about this? My regex is below

  // ' " cannot insert this characters

  RegExp specialCharReg= RegExp(r'[~!@#$%^&*()_+`{}|<>?;:./,=\-[]]');

  RegExp passwordReg = RegExp(
      r'^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`{}|<>?;:./,=\-[]])[a-zA-Z\d~!@#$%^&*()_+`{}|<>?;:./,=\-[]]{8,20}$');

input : ghktkl12! , ghktkl12] 
both are not working in specialCharReg, because of [] in regexp

Charles
  • 93
  • 7
  • Both `specialCharReg` and `passwordReg` neglect to escape `[` and `]`. Consequently `specialCharReg` tries to match one of those punctuation characters followed by a literal `]`. It should be ```RegExp(r'[~!@#$%^&*()_+`{}|<>?;:./,=\-\[\]]')```. – jamesdlin Mar 17 '22 at 02:58
  • thank you for your answer, I faced another problem. I prefixed `'`, `"`, ] with `\\`, and the ' or " doesn't work when I use r"^~~", or r'^~~', How can I use that two character together? – Charles Mar 17 '22 at 04:57
  • I don't understand what you're asking. "Doesn't work" isn't helpful. What happens? Do you mean that `RegExp(r'^~~')` doesn't match some input string as expected? If so, what is your input string? `print(RegExp(r'^~~').hasMatch('~~hello'));` prints `true` for me. – jamesdlin Mar 17 '22 at 05:00
  • not that mean. I wanna use `RegExp(r'\'');` this regex for the character `'`, but it doesn't work for `\'`. – Charles Mar 17 '22 at 05:16
  • `r'\''` will not work because the `r` prefix means it's a *raw* string that will not use string escape characters. See [escape " regexp in dart](https://stackoverflow.com/q/58141263/). (And what does that have to do with `r'^~~'`?) – jamesdlin Mar 17 '22 at 05:38
  • thank you for answer, and the r'^~~' means r'^ some regex(bla bla == ~~)', it doesn't have any special mean. – Charles Mar 21 '22 at 04:21

0 Answers0