2

:Statement

Say we have following three records, and we just want to match the first one only -- exactly one digital followed by a specific word, what is the regular expression can be used to make it(in NotePad ++)?

  1. 2Cups
  2. 11Cups
  3. 222Cups

The expressions I tried and their problems are:

  • Proposal 1:\d{1}Cups

it will find the "1Cups" and "2Cups" substrings in the second and third record respectively, which is what we do not want here

  • Proposal 2:[^0-9]+[0-9]Cups

same as the above

(PS: the records can be "XX 2Cups", "YY22Cups" and "XYZ 333Cups", i.e., no assumption on the position of the matchable parts)

Any suggestions?

:Reference

[1] The reg definition in NotePad++ (Same as SciTe)

As mentioned in Searching for a complex Regular Expression to use with Notepad++, it is: http://www.scintilla.org/SciTERegEx.html

[2] Matching exact number of digits

Here is an example: regular expression to match exactly 5 digits.

However, we do not want to find the match-able substring in longer records here.

Community
  • 1
  • 1
user3317307
  • 21
  • 1
  • 3

5 Answers5

2

If the string actually has the numbered sequence (1. 2Cups 2. 11Cups), you can use the white space that follows it:

\s\d{1}Cups

If there isn't the numbered list before, but the string will be at the beginning of the line, you can anchor it there:

^\d{1}Cups

Tested in Notepad++ v6.5.1 (Unicode).

Ken White
  • 123,280
  • 14
  • 225
  • 444
1

It sounds like you want to match the digit only at the start of the string or if it has a space before it, so this would work:

(^|\b)\dCups

Regular expression visualization

Debuggex Demo

Explanation:

  • (^|\b) Match the start of the string or beginning of a word (technically, word break)
  • \d Match a digit ({1} is redundant)
  • Cups Match Cups
elixenide
  • 44,308
  • 16
  • 74
  • 100
0

This will work:

\b\dCups

If "Cups" must be a whole word (ie not matching 2Cupsizes:

\b\dCups\b

Note that \b matches even if at start or end of input.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

I found one possible solution:

  1. Using ^\d{1}Cups to match "Starting with one digital + Cups" cases, as suggested by Ken, Cottrell and Bohemian.
  2. Using [^\d]\dCups to match other cases.

However, haven't found a solution using just one regex to solve the problem yet.

user3317307
  • 21
  • 1
  • 3
0

Have a try with:

(?:^|\D)\dCups

This will match xCups only if there aren't digit before.

Toto
  • 89,455
  • 62
  • 89
  • 125