0

I have two types of text from that i need to extract the name

1     #PRESTIGE COOKTOP 3B        6995.00/
3    #PRESTIGE SS DLX ALPHA       2250.00/

By using following expression I am able to extract the first one but not the second one.

I use

(?'SrNo'\d+)\s+(?'Itemname'#([A-Z\s.*-]*)[\d]{1}[A-Z\s.*-]*)\s+(?'MRP'[0-9.]*)
1

2 Answers 2

1

You can use

(?'SrNo'\d+)\s+(?'Itemname'#(\D*?)(?:\s+(\d[A-Z\s.*-]*?))?)\s+(?'MRP'\d[\d.]*)

See the regex demo. Details:

  • (?'SrNo'\d+) - Group "SrNo": one or more digits
  • \s+ - one or more whitespaces (use \h+ if you want to stay on the same line)
  • (?'Itemname'#(\D*?)(?:\s+(\d[A-Z\s.*-]*?))?) - Group "Itemname": #, Group 3: zero or more non-digit char as few as possible, then an optional occurrence of one or more whitespaces and then Group 4: a digit and then zero or more uppercase letters, whitespace, ., * or - chars as few as possible (maybe .* should be removed if you meant to match any text, or just replace [...] here with .*? to match any text)
  • \s+ - one or more whitespaces (use \h+ if you want to stay on the same line)
  • (?'MRP'\d[\d.]*) - Group "MRP": a digit and then zero or more digits or dots.
Sign up to request clarification or add additional context in comments.

1 Comment

@aniketashtekar Please check my explanation, since your .* inside the character class looks suspicious. Maybe (?'SrNo'\d+)\s+(?'Itemname'#(\D*?)(?:\s+(\d.*?))?)\s+(?'MRP'\d[\d.]*) will work even better, see this regex demo.
0

You have to remove ]{1}[ or else you have 2 character classes where the first character has to be a digit and in the second string ALPHA does not start with a digit.

(?'SrNo'\d+)\s+(?'Itemname'#([A-Z\s.*-]*)[\d]{1}[A-Z\s.*-]*)\s+(?'MRP'[0-9.]*)
                                            ^^^^^ 

The updated pattern:

(?'SrNo'\d+)\s+(?'Itemname'#([A-Z\s.*-]*)[\dA-Z\s.*-]*)\s+(?'MRP'[0-9.]*)

Regex demo

4 Comments

2 #Mixture 7oz/24 96.00/ for this text this is not working
@aniketashtekar like this? regex101.com/r/oQMEzS/1
no actually i want #Mixture 7oz/24 this as Itemname
@aniketashtekar Here are some patterns that get the values. If the forward slash at the end is always there: (?'SrNo'\d+)\s+(?'Itemname'#(.*?))\h+(?'MRP'\d[\d.]*)\/ See regex101.com/r/79WPdL/1 If it always ends in a number with a dot (?'SrNo'\d+)\s+(?'Itemname'#(.*?))\h+(?'MRP'\d+\.\d+\b) See regex101.com/r/wyxkvz/1 To allow word chars and horizontal whitespace chars in between the groups: (?'SrNo'\d+)\s+(?'Itemname'#([\w\h\/]+?))\h+(?'MRP'\d[\d.]*)\b See regex101.com/r/VzO8ys/1

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.