0

I have this sentence commerce_product--default which I want to extract only the last part so here default, to do so I'm using this RegEx RegExp(r'-\s*\K[^-]+$')

Now I try to display it in my code but I didn't find the method to do it, I've tried with stringMatch or replaceAll like this :

final type = 'commerce_product--default';
final newType = type.replaceAll(RegExp(r'-\s*\K[^-]+$'), type);

but it didn't work.
Thanks for the help

1
  • Why don't you replace before the last - with an empty string? ^.*- Commented Jul 30, 2020 at 10:33

3 Answers 3

2

To match everything after the last -, you should just match all trailing non-- characters:

var lastPartRE = RegExp(r"[^-]*$");
var lastPart = lastPartRE.firstMatch()?.group(0);

This matches all trailing non-- characters.

If the string does not contain any -, then it just matches the entire string. If you want a non-match in that case, you can put in a requirement for the ?:

var afterLatDashRE = RegExp(r"(?<=-)[^-]+$");

This would require a minus character before the match.

I'd probably just omit the RegExp entirely and do it with code:

String lastPart(String text) {
  return text.substring(text.lastIndexOf("-") + 1);
}
Sign up to request clarification or add additional context in comments.

Comments

1

To replace all before the last occurrence of - you could also match the whole line until the last occurrence of it and assert that what follows is 1+ times any char except - until the end of the string.

In the replacement use an empty string.

^.*-\s*(?=[^-]+$)

Regex demo | Dart demo

final type = 'commerce_product--default';
final newType = type.replaceAll(RegExp(r'^.*-\s*(?=[^-]+$)'), '');
print(newType);

Output

default

2 Comments

thanks for your comment! indeed it does work exactly like the first answer but it uses a bit less of code so I would use it instead!
@StefanYYC It's worth noting that this application requires a somewhat complex RegExp pattern to match but is fairly trivial using non-RegExp means. (See Irn's answer)
1

You are using a PCRE pattern that is not supported in Dart regex. You may use

final type = 'commerce_product--default';
final newType = RegExp(r'-\s*([^-]+)$').firstMatch(type);
if (newType!=null) {
  print(newType.group(1));
}

It is basically the same (see the regex demo), but without \K match reset operator, and the part you need to extract is wrapped with capturing parentheses. These parentheses create a group (here, with ID = 1) that you may access (with newType.group(1)) after a match is found (if (newType!=null) check is thus important).

I suggest using .firstMatch() here since you expect a single match at the end of the string.

1 Comment

Oh thanks for the info! I haven't been working with Dart a lot yet and I didn't know that. Funny enough I've got the Regex from one of your reply too ahaha! Just tested and it works as expected, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.