How do I identify in a string that a letter has a number beside?

waroxx

I need to verify that a string line contains a specific word (M3), and this word contains a number in it. The problem is that the number is not always the same. Is there a way to verify a number in Qt C++?

I tried this, obviously not working:

if (line.contains("M"+"%i")) {
    qDebug() << "contains the word";
}
Striezel

You could use a regular expressions to search for certain patterns in a string. Since C++11 the C++ language contains a regular expression library that can be used to work with regular expressions.

Simple example:

#include <iostream>
#include <regex>
#include <string>

int main()
{
  std::string s;
  //Fill s with the text you want to check for the pattern here!
  // I'll use a fixed value here, but you'll need to change that.
  s = "bla M5 bla";
  //regular expression for "letter M followed by one digit"
  std::regex myRegex("M\\d");
  //std::regex_search checks whether there is a match between the
  // given sequence s and the regular expression. Returns true, if
  // there is a match. Returns false otherwise.
  if (std::regex_search(s, myRegex))
  {
    std::cout << "Text contains the search pattern.\n";
  }
  else
  {
    std::cout << "Text does not contain the search pattern.\n";
  }
  return 0;
}

Of course, if your numbers can be more than one digit, you will have to adjust the regular expression accordingly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I slice a string in python based on the text, not the letter number?

From Dev

How do I find a combination of a letter a number in a VBA string?

From Dev

How do I check if a Java String contains at least one capital letter, lowercase letter, and number?

From Dev

How do I identify whether a number is an integer?

From Dev

How do i check if a string has a certain number of letters and digits?

From Dev

How do I select a string that has a word begins with a capital letter with a regex?

From Dev

How to search for a string in sql that starts in a specific letter and then has a number?

From Dev

how do i remove a letter from string

From Dev

How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only

From Dev

How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only

From Dev

How do I parse a string consisting of a letter and number idiomatically and efficiently in Rust?

From Dev

How do I place a <hr> beside an image?

From Dev

How can I count the number of occurrences in a string based on character letter?

From Dev

How can I show post number beside the category name in wordpress

From Dev

How do I check if a number has a remainder?

From Dev

How do I identify the variable's name as a string from self?

From Dev

How do I best identify a String that contains JSON in it?

From Dev

How can I find the number of 8 letter words that do not contain the letter "e", using the grep command?

From Dev

How do I make a single letter in a string uppercase

From Dev

How do I add a specific letter to a variable and then make it a string

From Dev

How do I create a method that capitalizes the first letter of a string in java?

From Dev

How do I grab each letter of a string in android?

From Dev

How do i make random letter from a string?

From Dev

How do i invert every odd letter in a string?

From Dev

How do I use padding if my string has odd number of characters in C?

From Dev

How do I put 2 columns beside each other

From Dev

How do I add the value beside the matched object?

From Dev

How do I capitalized any two letter word in a string in addition to the first letter of any word?

From Dev

How to identify a character within a word and substitute that letter into another string - Python

Related Related

  1. 1

    How do I slice a string in python based on the text, not the letter number?

  2. 2

    How do I find a combination of a letter a number in a VBA string?

  3. 3

    How do I check if a Java String contains at least one capital letter, lowercase letter, and number?

  4. 4

    How do I identify whether a number is an integer?

  5. 5

    How do i check if a string has a certain number of letters and digits?

  6. 6

    How do I select a string that has a word begins with a capital letter with a regex?

  7. 7

    How to search for a string in sql that starts in a specific letter and then has a number?

  8. 8

    how do i remove a letter from string

  9. 9

    How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only

  10. 10

    How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only

  11. 11

    How do I parse a string consisting of a letter and number idiomatically and efficiently in Rust?

  12. 12

    How do I place a <hr> beside an image?

  13. 13

    How can I count the number of occurrences in a string based on character letter?

  14. 14

    How can I show post number beside the category name in wordpress

  15. 15

    How do I check if a number has a remainder?

  16. 16

    How do I identify the variable's name as a string from self?

  17. 17

    How do I best identify a String that contains JSON in it?

  18. 18

    How can I find the number of 8 letter words that do not contain the letter "e", using the grep command?

  19. 19

    How do I make a single letter in a string uppercase

  20. 20

    How do I add a specific letter to a variable and then make it a string

  21. 21

    How do I create a method that capitalizes the first letter of a string in java?

  22. 22

    How do I grab each letter of a string in android?

  23. 23

    How do i make random letter from a string?

  24. 24

    How do i invert every odd letter in a string?

  25. 25

    How do I use padding if my string has odd number of characters in C?

  26. 26

    How do I put 2 columns beside each other

  27. 27

    How do I add the value beside the matched object?

  28. 28

    How do I capitalized any two letter word in a string in addition to the first letter of any word?

  29. 29

    How to identify a character within a word and substitute that letter into another string - Python

HotTag

Archive