I'm trying to grab just the id number from a Facebook picture URL using the replaceall regex method in Dart. What should I be using instead of $2 in the following code? The id number I need is between the "asid=" and "&height".
void main() {
String faceavatar = 'https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10153806530149154&height=50&width=50&ext=1596623207&hash=AeSi1yDvk8TCqZql';
String currentavatar = faceavatar.replaceAll(RegExp('(.*asid=)(\d*)height.*'), $2;
print(currentavatar);
}
You may try:
.*?\basid\b=(\d+).*
Explanation of the above regex:
.*?
- Lazily matches everything except new-line before asid
.\basid\b
- Matches asid
literally. \b
represents word boundary.=
- Matches =
literally.(\d+)
- Represents first capturing group matching digits one or more times..*
- Greedily everything except new-line zero or more times.$1
- For the replacement part you can use $1
or match.group(1)
.You can find the demo of above regex in here.
Sample Implementation in dart:
void main() {
String faceavatar = 'https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10153806530149154&height=50&width=50&ext=1596623207&hash=AeSi1yDvk8TCqZql';
String currentavatar = faceavatar.replaceAllMapped(RegExp(r'.*?\basid\b=(\d+).*'), (match) {
return '${match.group(1)}';
});
print(currentavatar);
}
You can find the sample run of the above implementation in here.