0

I want to write a regular expression in a C++ program which checks if a string matches the following expression:

a word not containing '_' but it can contain number followed by

'_' followed by

three digits in a row (i.e. 047)

followed by '_' followed by

a string (can contain anything)

I have tried the following expression but it does seem to find the desired string as described above. I suspect the problem lies in the first part but I cannot detect it in order to modify properly:

static const wregex stringForm("([^_]?)_?(\\d{3})_(.+)");  

What is then the proper reg expression?

6
  • 1
    Could you add a concrete example of a string you want matched? Commented Jul 24, 2012 at 12:36
  • The first to conditions contradict. "Not containing ... " vs. "can contain ... when followed by ...". This logical error reflects in your regexp. Commented Jul 24, 2012 at 12:40
  • your example does not match the above description (the description does not ask for leading underscore before 3 digits number). You should provide several examples, some matching, some not matching to maximize changes to get a usefull answer. Commented Jul 24, 2012 at 12:54
  • your example does not have "number followed by '_' followed by three digits" Commented Jul 24, 2012 at 14:42
  • @OrangeDog the first part of the string should be a word which can contain number as well, not absolutely a number. Commented Jul 25, 2012 at 7:35

2 Answers 2

2
\b[^_]*?(_\d{3}.+?)?\b

A word (\b is word boundary, quantifiers are non-greedy). Zero or more characters that aren't _ ([^_]*?). Optionally ((...)?), the digit sequence you described (_\d{3}) followed by one or more of any character (.+?).

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried this:

static const wregex stringForm("([a-zA-Z0-9]*_[0-9]{3}.*)"); 

Comments

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.