Reputation: 349
I have the following regular expression that finds invoice number from the string.
(?:invoice)(?:#\s|:|#|\s*|-|\s:|\s#|\s-\s|\s#\s|\s\()(\d+)
Sample data:
ABC invoice#123456 this is an invoice
ABC invoice-123456 this is an invoice
ABC invoice 123456 this is an invoice
The regular expression takes care of different formats and works fine. What I would like to do is to negate the invoice number and return everything else.
Is it even possible? for e.g The regular expression should return following text without invoice number.
ABC invoice# this is an invoice
ABC invoice- this is an invoice
ABC invoice this is an invoice
Thanks is in advance.
Upvotes: 1
Views: 59
Reputation: 521299
You can try matching with the following pattern:
^(.*?invoice[^0-9]*)\d+(.*)$
And then replace with the first and second capture groups:
$1$2
You never told us what language/tool you are using, but this approach should work in most languages and tools, such as Java or Notepad++.
Edit
It actually is possible to do this replacement in Java without using capture groups. Instead, we can use lookaheads to pinpoint the invoice number to be removed:
String input = "ABC invoice#123456 this is an invoice";
input = input.replaceAll("(?<=invoice.)\\d+(?=\\s)", "");
System.out.println(input);
ABC invoice# this is an invoice
Upvotes: 1
Reputation: 176
.*(?=(?:invoice)([?:#\- ]*)(\d+))|(?<=(?:invoice)([?:#\- ]*)(\d+))[ ].*
Regex Demo - Negate Invoice String
For the regex that match invoice string:
(?:invoice)(?:#\s|:|#|\s*|-|\s:|\s#|\s-\s|\s#\s|\s\()(\d+)
You can simplify to: (?:invoice)([?:#\- ]*)(\d+)
Upvotes: 3