Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

String Pattern Match in C

Hello HF,
So i am tying to match a pattern in a string.And extract a word for the string.
For example:
In a huge string I want to search for, "admin-* admin-1"
I want to search for this pattern in a huge array and the * can be anything.And if the pattern matches.I want to get the value *. As in if the string was "admin-lotty admin-1" I want to get the word "lotty".Can somebody please help me with it and show me how to do it please.

Best Regards,
geekmorpheus403
If you have some basic C++ knowledge you can probably end up writing something like this. I'd recommend starting by writing a program that actually matches the exact string and then go from there. Don't try to take in the entire problem at once, split it up into parts and work on it.
Code
#include <iostream>
#include <regex>

int main(){
    std::string str = "potty admin-lotty admin-1 snotty";

    if (std::regex_match(str, std::regex(".*(admin-).*( admin-1).*") )){
      size_t pos;

      pos = str.find("admin-");
      if (pos != std::string::npos){
      str.erase(str.begin(), str.begin() + 6 + (int)pos);
      }

      pos = str.find(" admin-1");
      if (pos != std::string::npos){
      str.erase(str.begin() + (int)pos, str.end());
      }

      std::cout << "string str = \"" << str << "\"" << std::endl;
    }

    return 0;
}
[Image: 4bj39qK.png]
He asked for a C solution.
Try hxxps://github.com/cesanta/slre
Examples included on the frontpage.
Guy asks for help learning something community posts code without explanation (in wrong language this time.)

"Why do people think HF is a joke??? Isn't what we're doing right?"

[Image: DbFZvBK.jpg]
(Nov 25, 2014 04:47 AM)-ceramic- Wrote:  He asked for a C solution.
Try hxxps://github.com/cesanta/slre
Examples included on the frontpage.

Thanks man.But it is working fine with small string.But in for huge string(60000) characters it is not ending for some reason.I left it for hours but the program never stops. Can you help?
Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int ret;
  char format[256], token[50];
  char buf[] = "admin-lotto admin=1";

  ret = sprintf(format, "admin-%%%us admin=1", sizeof(token));
  if (ret < 0) exit(1);

  ret = sscanf(buf, format, token);
  if (ret <= 0) exit(1);

  printf("token: %s\n", token);
}

Simple example. Are you looking for multiple occurrences? Also, you'll have to use other functions to search for the index of 'admin-' in the buffer. If you're paranoid that your buffer size might not be large enough, then you can dynamically allocate based on the length of the string from the first character after the '-' up until the first space if it exists, and if the pattern is a match. Obviously this eats up CPU time too.

sscanf() is an easy solution as an alternative to parsing and pattern matching manually.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by PolaR01May 6, 2020 08:06 AM 3 174 May 6, 2020 08:06 AM
Last Post: Joel
[.NET] by CendrixMar 21, 2020 09:24 AM 8 328 Mar 21, 2020 09:24 AM
Last Post: Cendrix
[C] by MordiciaNov 15, 2019 07:21 PM 3 209 Nov 15, 2019 07:21 PM
Last Post: Mordicia