I'm trying to catch symbols in a dart Regexp. My regexp looks like this:
RegExp containsSymbolRegExp = RegExp(r"[-!$%^&*()_+|~=`{}\[\]:;'<>?,.\/]");
However, I also need to make it catch the symbol " I can't stick " in there though, since it messes with the string. Any ideas how to do this? Thanks.
edit: jamesdlin's answer works great for dart. But it doesn't work for my flutter app, so I'm thinking it has something to do with flutter. Here's the code I'm applying this to:
TextEditingController _passwordController = TextEditingController();
bool containsSymbol;
RegExp containsSymbolRegExp = RegExp(r"[-!$%^&*()_+|~=`{}#@\[\]:;'<>?,.\/"
'"'
"]");
void _handleChange(text) {
setState(() {
containsSymbol = containsSymbolRegExp.hasMatch(_passwordController.text);
});
print(containsSymbol); // always prints false for " ' \ but works for all other symbols
}
Widget _textField(controller, labelText) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(labelText, style: TextStyle(fontSize: 11)),
Container(
width: MediaQuery.of(context).size.width * 0.9,
height: 35,
child: TextField(
onChanged: _handleChange,
style: TextStyle(fontSize: 20),
keyboardType: TextInputType.text,
controller: _passwordController,
cursorColor: Colors.grey,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey))),
),
),
],
);
}
You could take advantage of string concatenation of adjacent string literals and either use a single-quoted string or use a non-raw string with "
escaped:
RegExp(
r"[-!$%^&*()_+|~=`{}\[\]:;'<>?,.\/"
'"'
"]");
Alternatively you always could use a non-raw string with more escapes:
RegExp(
"[-!\$%^&*()_+|~=`{}\\[\\]:;'<>?,.\\/\"]");
although that's messier.