-2

Ive got this string

designer-brand-paradise-design-t-9999-123990-tshirt-print-18-preorder-november-delivery

I need to extract any instance of preorder-* from it as I have a bit of a situation with alot of data lingering around.

Im new to regular expressions, so Im hoping this is an easy one. any help greatly appreciated

user125264
  • 1,809
  • 2
  • 27
  • 54
  • You want all the text following the word "preorder-"? – Matthew Gunn Nov 25 '15 at 00:00
  • 2
    Use `preorder-.*$`. The `$` (end of string anchor) isn't strictly necessary since the repetition operator matches greedily. However, it documents your intent. You will usually refer to the matched data by `$0`, but the details depend on the syntax of the host language and the regex engine. – collapsar Nov 25 '15 at 00:00
  • What environment are you in btw? Java? PHP? Python? – Matthew Gunn Nov 25 '15 at 00:02
  • @collapsar I'd have to check docs specific to each environment, but I think there can be a difference sometimes with a newline character. Sometimes . *doesn't* match newline characters. preorder-.* would match everything up to the newline while preorder-.*$ wouldn't find a matching section in the string "preorder-asdf\nblahblah" (at least not in Java unless you had compiled with Pattern.DOTALL) – Matthew Gunn Nov 25 '15 at 00:09
  • 1
    @MatthewGunn You are right. i tacitly assumed based on the OP's example that newlines would not occur in the tested strings. Apart from the dotall Option (fwiw, also accessible through the inline option `(?s)`,`(?-s)` for parts of the match pattern), the multiline modifier (`Pattern.MULTILINE`, `(?m)`) that makes the anchors match line start/end may also be used. – collapsar Nov 25 '15 at 08:04

1 Answers1

1

Looks like the regex you want to grab everything following preorder is:

preorder-(.*)

This will match the string preorder- and then everything past that is put into a group. Eg. in Java this would go:

Pattern preorder_pattern = Pattern.compile("preorder-(.*)");
Matcher m = preorder_pattern.matcher(my_string);
if(m.find()) {
    // String my_string matches the pattern!
    String stuff_after_preorder = m.group(1);
}
Matthew Gunn
  • 4,451
  • 1
  • 12
  • 30