I need to extract a sub string inside a dynamic input, I've achieved the output I need, but it's only pure hard code, so it's not that dynamic and reliable. Is there any other way for me to extract the part "B1003 = Engineering Business Card" (Item Description) & "2"(Quantity), these are both dynamic, an entirely different item could be input such as; "O1003 = Pencil", "O1004 = Sticky Notes". Is there a way to code this in regex that would enable a more reliable code?
The input being read here is from an extracted text using Tesseract OCR, I need to extract the needed information and pass it to another service.
var requisition = `Lines
Line Item Description Category Name Quantity UOM Price Amount (USD) Status Funds Status //this line is static
1 B1003 = Engineering Business Card Business Cards 2 Ea 50.00USD 100 Pending Approval Not Reserved //this line is dynamic
Requester Jay Doe Supplier ABC Corp //this line is static
Lines
Line Item Description Category Name Quantity UOM Price Amount (USD) Status Funds Status //this line is static
1 O1003 = Pencil Office Supplies 5 Ea 50.00USD 100 Pending Approval Not Reserved //this line is dynamic
Requester Jay Doe Supplier ABC Corp //this line is static
`;
//rule 1 - Gets all Items + Quantity
//rule 2 - Gets all Items
//rule 3 - Gets all Quantity
//resultArray - Contains Quantity + Item e.g. 2 B1003 Engineering Business Cards
var rule1 = /(B1002 = Accountant Business Card|B1003 = Engineering Business Card|B1001 = Sales and Marketing Business Card|O1001 = Black Ballpen Branded Panda Regular with Eraser|O1002 = Notebook|O1003 = Pencil|O1004 = Stick Notes) (.*) ([0-9]|[0-9][0-9]|[0-9][0-9][0-9])/
var rule2 = /(B1002 = Accountant Business Card|B1003 = Engineering Business Card|B1001 = Sales and Marketing Business Card|O1001 = Black Ballpen Branded Panda Regular with Eraser|O1002 = Notebook|O1003 = Pencil|O1004 = Stick Notes)/
var rule3 = /([0-9]|[0-9][0-9]|[0-9][0-9][0-9])/
var resultarray = []
var stringarray = requisition.split("\n")
stringarray.forEach(element => {
var result = element.match(rule1)
if (result!=null){
var itemName = result[0].match(rule2)
var quantity = result[0].match(rule3)
resultarray.push (quantity[0]+ " " + itemName[0])
}
});
console.log (resultarray.join(", "))
Note: Just to make things clearer, this is the image I'm extracted the text from Legend: Blue - Static Unboxed - Dynamic Yellow - Text needed to be extracted (Also dynamic)
This is the image extracted, first line is static, second line is dynamic
Expected result is 2 B1003 = Engineering Business Card(, B1002 = Accountant Business Card - will output if there is a similar item in the code) Please check the comments on requisition variable.
Again, I can already get the desired output, I just need to know how the code can be done differently and more dynamically and reliable using RegEx. Please bear with me as I don't know much about RegEx. Thanks!
([0-9]|[0-9][0-9]|[0-9][0-9][0-9])should be probably written as(\d{1,3})or(\d+). The rest is too vague. Please provide exact requirements for the pattern you need to match.new RegExp(variable).