Batakj
Batakj

Reputation: 12763

Regular Expression for a alphanumeric after a text

This is my regular expression

(\b(serial|sheet))+(\s(number|code|no))+?\b

For the input :

Serial no
sheet no
Sheet Number

Requirement is to parse the text which contain:

Serial no : 2424ABC
Sheet No 5 (Without colon)
Sheet No : 5
Serial No = 5335ABC

How to escape a assignment character (if available) and parse the next alphanumeric character?

Upvotes: 0

Views: 90

Answers (4)

NeverHopeless
NeverHopeless

Reputation: 11233

Try the following pattern:

(serial\s+no|sheet\s*no)(\s*\:\s*)([a-z0-9]+)

Demo.

You can add further cases to the pattern in first group. I covered two cases separated by |.

You can find the alphanumeric value in last group of this pattern.

Please note that, this pattern is written as a case-insensitive pattern.

Upvotes: 0

AdrianHHH
AdrianHHH

Reputation: 14076

If the = is optional, you could replace the \s in the regular expression with [=\s] to allow either a space or an equals. Perhaps better and matching your example try \s=?\s*.

If may characters might be between the word and the number then perhaps use \s[-=#~_]?\s*. Note the - goes at the start, otherwise it will be interpreted as a range of characters. Namely [a-f] means [abcdef], ie any of those six characters, whereas [-af] means any of those three characters.

Hence the regular expression becomes:

(\b(serial|sheet))+(\s[-=#~_]?\s*(number|code|no))+?\b

Upvotes: 0

rap-2-h
rap-2-h

Reputation: 32058

This should work:

(\b(serial|sheet))+(\s(number|code|no))+?\b\s*[:=#~– ]*(.*)

You can try it here : https://regex101.com/r/rO2cX1/1

Upvotes: 1

Rahul Desai
Rahul Desai

Reputation: 15501

To escape a assignment character, do \=.

To parse the alphanumeric characters, do [a-zA-Z0-9]* or simply \w*.

Upvotes: 0

Related Questions