Reputation: 667
How are you?
I am a beginner in dart and flutter and I am also a new programmer and It's the first time I ask a question on StackOverflow.
I am training now on regular expressions and I am trying to create a one to match all characters with hashtag sign like:
#I_am_here
#Stop
anyway, everything is going okay but actually, when I try to replace the string which is in English with Arabic I get stuck.
void main() {
String text = "#اسمي_هنا";
RegExp exp = new RegExp(r"\B#\w\w+" , unicode: true , multiLine: true);
exp.allMatches(text).forEach((match) {
print(match.group(0));
if (match.group(0) == null) {
print(null);
}
});
}
it doesn't even prints null.
Upvotes: 3
Views: 4762
Reputation: 795
TextField
(
inputFormatters:
[ WhitelistingTextInputFormatter(RegExp("[a-z\u0621-\u064a-\ ]",unicode: true)), ],
)
Upvotes: 4
Reputation: 52185
The \w
character is shorthand for [A-Za-z0-9_]
. In your case, you are using an Arabic Alphabet, so the expression will not match, since A-Z
belongs to a Latin Alphabet.
One way to go around it is to use \p{L}
, this will basically match any letter, regardless of language. For more information on the topic, you can looking into regular expressions with an interest in unicode matches.
EDIT:
As per your comment, the issue with this section of the expression: #\p{L}+[_]+\p{L}+
, the engine is expecting a hash, letters (one or more), followed by an underscore (one or more) followed by letters (one or more).
In your other example, wherein we can have the following form of string: #foo
and #foo_bar
, we need to ammend the expression such that the _bar
section is optional. To do this, we alter the expression as follows: #\p{L}+([_]+\p{L}+)?
. In this case, the engine is now expecting either #foo
or #foo_bar
. If you need to match anything of the form: #foo_bar_hello
, you can also use #\p{L}+([_]+\p{L}+)+
.
Upvotes: 3
Reputation: 667
After tries to actually do what I want I found that the best solution is:
RegExp exp = new RegExp(r"([#][^\s#]*)");
String str = "#الالاا_سيسيسشي_لأيبيبالابل";
exp.allMatches(str).forEach((m) {
print(
m.group(0),
);
});
Upvotes: 3