3

Regex pattern used:

(?<!cart .*)(?<!order .)(\b\d{4,12}\b)

Examples:

so the cart number is 12344567 that it
my cart number is 1234
order number note down 12345
order 12345
credit card is 0000
4444
the 5456

Expected output:

0000
4444
5456

Here i don't want number that starts with word order or cart that can range from 4 to 12.

1

1 Answer 1

2

If you want to use Python with a infinite quantifier in the lookbehind, you can use the PyPi regex module.

You don't need the capture group, and you have to repeat the dot for the order part (?<!order .*)

But using an alternation, you can use a single lookbehind:

(?<!(?:cart|order) .*)\b\d{4,12}\b

See a regex demo.

Example

import regex

pattern = r"(?<!(?:cart|order) .*)\b\d{4,12}\b"

s = ("so the cart number is 12344567 that it\n\n"
    "my cart number is 1234\n\n"
    "order number note down 12345\n\n"
    "order 12345\n\n"
    "credit card is 0000\n\n"
    "4444\n\n"
    "the 5456")

print(regex.findall(pattern, s))

Output

['0000', '4444', '5456']

Another option is to use an alternation with re.findall and filter out the empty values, or use re.finditer and loop the values checking for a group 1 value.

An example with re.findall, where you match what you don't want and capture in group 1 what you want to keep using a for comprehension:

import re

pattern = r"^.*\b(?:cart|order)\b.*|(\b\d{4,12}\b)"

s = ("so the cart number is 12344567 that it\n"
            "my cart number is 1234\n"
            "order number note down 12345\n"
            "order 12345\n"
            "credit card is 0000\n"
            "4444\n"
            "the 5456")

print([s for s in re.findall(pattern, s, re.M) if s])

Output

['0000', '4444', '5456']
Sign up to request clarification or add additional context in comments.

13 Comments

Any other alternative ways is there other than PY regex?
@Dip I have added another solution with Python re
i have code i want number not starting with order and card as well as it should not end with dollar also can we acheive expected output by passing one example at a time using re module import re pattern = "(^.*\b(?:cart|order)\b.*)(\b\d{2,12}\b)" text1 = ('the cart number is 1234 and 4567 that it!') text2 = ("order number note down 12345") text3 = "credit card is 0000" text4 = "the 4567" text5 = "i got 245 dollar" result = re.search(pattern, text4) print(result) Expected output match text3 0000 text4 4567
@Dip If you want to do that with re.search and get a single match, then you can do it like this ideone.com/ynXr8M
Hi Actually i dont want to match number followed by words dollar or points as well. I tried with your provide code in re.findall() but i am facing issue. Can you help me on this. Thank you
|

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.