How to extract the unmatched portion of a string using a regex

Bruno

I'm trying to display some messages to the user when the input is invalid.

I wrote this regex, to validade this pattern : (Name of 10 characters) (Number between 0-9)

e.g. Bruno 3

^([\w]{1,10})(\s[\d]{1})$

When the user input any invalid string, Is it possible to know what group is invalid and print a message? Something like that:

if (regex_match(user_input, e))
{
  cout << "input ok" << endl;
}
else
{
    if (group1 is invalid)
    {
        cout << "The name must have length less than 10 characters" << endl;
    }

    if (group2 is invalid)
    {
        cout << "The command must be between 0 - 9" << endl;
    }
}
Shakiba Moshiri

As I see you want to match 1 to 10 character then a single space and then a single digit but in 2 group

here is what you want:

^([a-zA-Z]{1,10})( \d)$

NOTE

\w is equivalent to [a-zA-Z0-9_]
So if you need only 10 characters you should use [a-zA-Z] not \w


C++ code

std::string string( "abcdABCDxy 9" );

std::basic_regex< char > regex( "^([a-zA-Z]{1,10})( \\d)$" );
std::match_results< std::string::const_iterator > m_result;

std::regex_match( string, m_result, regex );
std::cout << m_result[ 1 ] << '\n';   // group 1
std::cout << m_result[ 2 ] << '\n';   // group 2   

the output

1abcdABCDxy
 9

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Need a regex to extract a portion of the string using java

From Dev

Python regex to extract a portion of string

From Dev

How to extract operators from specific portion of a linear expression using regex

From Dev

How I use REGEX to extract a portion of a string and add it to another column and then group by?

From Dev

Extract only the portion of a string between two regex patterns

From Dev

Extract only the portion of a string between two regex patterns

From Dev

How to extract numbers from a string using regex?

From Dev

How to extract substring from a string using regex?

From Dev

how to extract portion of a string between two substrings in a multiline string in python

From Dev

Extract a specific portion from a string(filename) using batch file

From Dev

How to extract string in regex

From Java

How to use regex to replace unmatched parentheses in a string with no nested parentheses?

From Dev

Replace a specific portion of string after a specific starting substring using regex

From Dev

Simplest way to extract a portion of a string?

From Dev

How can I use regex to remove a very specific portion of a string:

From Dev

Regex returning only portion of a string

From Dev

extract string using ruby regex

From Dev

Extract string if found using regex

From Dev

extract number in string using regex

From Dev

How to extract and replace a particular pattern from string using regex?

From Dev

How to extract multiple string values from a sentence using regex?

From Dev

How to extract numbers included in a string in order using regex

From Dev

How to extract numbers from string in Javascript using regex

From Dev

How to extract a number from a string using grep and regex

From Dev

Javascript: How to extract multiple values from a string using regex?

From Dev

How to extract onChange method's String parameters using Regex?

From Dev

How can i extract last shortest string using regex in java

From Dev

Javascript: How to extract multiple values from a string using regex?

From Dev

How to extract numbers from string in Javascript using regex

Related Related

  1. 1

    Need a regex to extract a portion of the string using java

  2. 2

    Python regex to extract a portion of string

  3. 3

    How to extract operators from specific portion of a linear expression using regex

  4. 4

    How I use REGEX to extract a portion of a string and add it to another column and then group by?

  5. 5

    Extract only the portion of a string between two regex patterns

  6. 6

    Extract only the portion of a string between two regex patterns

  7. 7

    How to extract numbers from a string using regex?

  8. 8

    How to extract substring from a string using regex?

  9. 9

    how to extract portion of a string between two substrings in a multiline string in python

  10. 10

    Extract a specific portion from a string(filename) using batch file

  11. 11

    How to extract string in regex

  12. 12

    How to use regex to replace unmatched parentheses in a string with no nested parentheses?

  13. 13

    Replace a specific portion of string after a specific starting substring using regex

  14. 14

    Simplest way to extract a portion of a string?

  15. 15

    How can I use regex to remove a very specific portion of a string:

  16. 16

    Regex returning only portion of a string

  17. 17

    extract string using ruby regex

  18. 18

    Extract string if found using regex

  19. 19

    extract number in string using regex

  20. 20

    How to extract and replace a particular pattern from string using regex?

  21. 21

    How to extract multiple string values from a sentence using regex?

  22. 22

    How to extract numbers included in a string in order using regex

  23. 23

    How to extract numbers from string in Javascript using regex

  24. 24

    How to extract a number from a string using grep and regex

  25. 25

    Javascript: How to extract multiple values from a string using regex?

  26. 26

    How to extract onChange method's String parameters using Regex?

  27. 27

    How can i extract last shortest string using regex in java

  28. 28

    Javascript: How to extract multiple values from a string using regex?

  29. 29

    How to extract numbers from string in Javascript using regex

HotTag

Archive