1

i want to match a very simple number-bar-number pattern : 1/2

My regex is: ([0-9]{1}\/[0-9]{1})

The problem is that I match things I want to exclude. I need exact matching excluding the rest.

My regex return as valid patterns as :

1/12344

2/23ABC

2/233423/2425

[update]

tested with some txt files using GREP, still having issues. By instance:

2/3/16 (it's a date and it matches the pattern, so grep returned the entire line)

I'm not very versed on regex so any help would be very much appreciated

Regards

useRj
  • 1,232
  • 1
  • 9
  • 15
  • 1
    So, to get this straight, you do not want `1/2344` to match, but it is matching? Also, with your current RegEx, you can replace `[0-9]` with `\d` and remove the `{1}` (typing it once is the same, so there is **never** any need to use `{1}`) – Kaspar Lee Apr 21 '16 at 09:57
  • `/\b[0-9]\/[0-9]\b/` or `/\b\d\/\d\b/` – Wiktor Stribiżew Apr 21 '16 at 10:01
  • Yep, that's the issue. The matching pattern keeps getting a lot of undesired strings that, matched the rule, but I don't need. Word boundaries!!!! that's the key. Thanks a lot – useRj Apr 21 '16 at 10:02
  • Still having issues. Check this string: 2/1/16 it's a date and it's matched. Grep gives me the entire string if found, so something else is needed to exclude it. – useRj Apr 21 '16 at 11:17

1 Answers1

0

Try this

(?:^|\s)(\d+\/\d+)(?=\s|$)

Regex demo

Explanation:
(?: … ): Non-capturing group sample
^: Start of string or start of line depending on multiline mode sample
|: Alternation / OR operand sample
\: Escapes a special character sample
( … ): Capturing group sample
+: One or more sample
(?=…): Positive lookahead sample
$: End of string or end of line depending on multiline mode sample

Tim007
  • 2,557
  • 1
  • 11
  • 20