3

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))),
        ),
      ),
    ],
  );
}
wei
  • 557
  • 1
  • 10
  • 24
  • Usually you should ask a separate question instead of editing it into an existing question. As for your new question: there's no reason why it shouldn't work in Flutter. Are you sure that a straight quote is entered and not, say, "smart" curly quotes? – jamesdlin Sep 29 '19 at 02:12
  • I see. Sorry, thanks. After testing, I'm almost certain it's because my text input is a "smart" curly quote. Knowing this, is there a way to restrict the textfield input or something? Thanks. – wei Sep 29 '19 at 16:31
  • If you want to restrict input to, say, alphanumeric characters, then you should specify what characters you allow instead of which you disallow. – jamesdlin Sep 29 '19 at 17:01

4 Answers4

7

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.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • Thanks, but unfortunately it didn't work for me :( – wei Sep 28 '19 at 14:57
  • @wei What happened when you tried it? It works for me: https://dartpad.dartlang.org/78c388a2cf55bce610c8fd31c94d0058 – jamesdlin Sep 28 '19 at 15:28
  • Hm, it works on dartpad great but when I use the Regexp in conjunction with my Flutter app and textEditingController, it still doesn't show a match for the " character. Maybe it is something to do with the textEditingController. I will investigate further, thanks again for the help! – wei Sep 28 '19 at 19:04
  • I edited my main question to be more specific, if you would like to take a look at it – wei Sep 28 '19 at 19:11
  • The concatenation method worked for me. However, the I couldn't escape `"` with `\"` :( – Valentin Vignal Jul 10 '20 at 15:17
  • @ValentinVIGNAL Without seeing exactly what you did when trying to escape, I can't help you. Escaping using the exact code I gave in my answer works in DartPad. – jamesdlin Jul 10 '20 at 17:51
7

Better solution is to use triple " for string starting and ending.

In such case one " will not end the string.

Try this:

RegExp containsSymbolRegExp = RegExp(r"""your_regexp_here""");

Can also be done with apostrophes like this:

RegExp containsSymbolRegExp = RegExp(r'''your_regexp_here''');
Maxgmer
  • 456
  • 8
  • 19
  • 1
    the triple """ helps a lot in this case. – petras J Jan 13 '22 at 07:22
  • In case this still doesn't work, it is likely due to the Smart Punctuation feature in ios. Refer to [this thread](https://stackoverflow.com/questions/70737696/how-to-disable-smart-punctuation-in-flutter-textfield-problem-when-a-string-co) – Tox Dec 31 '22 at 03:21
4

To answer to the edited part of your question being that it worked in Dart but not in Flutter...

It is because apostrophes/quotation marks from TextField values in Flutter are right quotation marks and therefore come in different Unicode.


For apostrophes, it's U+2019 () instead of U+0027 (')

For quotation marks, U+201D () instead of U+0022 (")


The Regex you were trying with is not capturing for such characters. Therefore, you just need to add those into your Regex.

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

Tip: Debug! :) I can already see some part of the value looking funny the moment it hits the breakpoint of where the value was about to be evaluated.

benChung
  • 366
  • 4
  • 9
0

To be a little more complete,

RegExp containsSymbolRegExp = RegExp(
  r'[!@#$%^&*()~`[=+-_\]/,.?":;{}|<>'
  '≥÷≤∞¢£™¡¶•ªº≠‘“∂ƒ©∆˚¬…æ√∫˜µ≈çœ∑´®†¥¨ˆø–π”’'
  "']",
);