Mark
Mark

Reputation: 13

How to use a Regular Expression to find a Specific word and return following 10 characters?

I need to find a regular expression that finds "Order #" and then return the following 10 characters.

For example I can the following rows (Ignore row numbers just using them to designate that it is a new or next line in the original data):

Row 1 Order #100013661 By John DOE
Row 2 REFUND for CHARGE(Order #100013667 By Lara Croft
Row 3 Order #100013668 By Sammy
Row 4 Blah Blah Blah Order #10013664 By Fluffy fluff

I want the expression to return:

ROW 1 100013661
ROW 2 100013667
Row 3 100013668
Row 4 100013664

Upvotes: 1

Views: 187

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

Use capturing groups for that:

Order #(.{9})

Use the tools in your hosting language to harvest the capturing group.

Demo.

Upvotes: 1

markbernard
markbernard

Reputation: 1420

Order #(.{10}) or Order #(.{1,10}) if it could be up to 10 characters.

Order #(\d{1,10}) if they are always numbers.

Upvotes: 0

Mikhail Burshteyn
Mikhail Burshteyn

Reputation: 5002

The regex you need is

(?<=Order #).{10}

Detailed explanation:

  • (?<=Order #) is a positive lookbehind: it matches if the literal string Order # occurs before current position;
  • .{10} matches any 10 characters.

Note that this won't match if your line has less than 10 characters in a line after the search string. If you need to match up to 10 characters, not exactly 10 characters, replace {10} with {1,10}.

Here is a demo.

Upvotes: 0

Related Questions