How to find a substring using str.find or regex?

I am trying to process all lines containing /* Test number */ in a c++ file using python.

For example a.cpp:

int main(void)
{
    /* Test 1 */          //will be found, and replaced.
    int a =1;

    /* Test 2 */          //will be found, and replaced.
    int b = 2;

    return 0;
 }

In my python, I tried:

with open(fname, 'rw') as f:
    for line_term in f:
        line = line_term.rstrip('\n')
            if(re.match('/\*\s[Test]\s\d+\*/', line):
                print line

But I got no print out at all. I am kind of new to regex expression, please give your suggestions.

I corrected your regex and the if statement syntax.

with open(fname, 'rw') as f:
for line_term in f:
    line = line_term.rstrip('\n')
        if re.match('\/\* Test \d+ \*\/', line):
            print line

String Manipulation and Regular Expressions, Finding and replacing substrings�. If you want to find occurrences of a certain character in a string, the find() / rfind() , index()� Using Regex Operator # Another option to determine whether a specified substring occurs within a string is to use the regex operator =~. When this operator is used, the right string is considered as a regular expression. The period followed by an asterisk .* matches zero or more occurrences any character except a newline character.

re.match starts matching at the beginning of the string so you could start your pattern with matching one or more spaces.

You can omit the square brackets around [Test] because that means the characters are in a character class and that would match any of the listed characters which could also be written as [Ttes].

Note that there is a space missing after matching the digits and using \s will also match a newline which might be unwanted if you want to only match characters on the same line.

For clarity the spaces are between square brackets but they don't need to be.

[ ]+/\*[ ]Test[ ]\d+[ ]\*/

Regex demo

Your code could look like:

with open(fname, 'rw') as f:
    for line_term in f:
        line = line_term.rstrip('\n')
        if(re.match(' +/\*\sTest \d+ \*/', line)):
            print (line)

JavaScript search(): Search for a substring using a Regular , The search() method accepts a regular expression and returns the index of the first match in a string: let index = str.search(regexp);. In this syntax, the regexp� Find Substring within a string that begins and ends with paranthesis. Comments. Post Posting Guidelines Formatting - Now. Top Regular Expressions.

use search() instead of match() because re.match() will only match at the beginning of the string, also you can use re.sub() to match and replace strings in one step:

with open(fname, 'r') as f:
    for line_term in f:
        line = line_term.rstrip('\n')
        if(re.search(r'/[*] Test \d+ [*]/', line)):
            print (line)

output:

    /* Test 1 */          //will be found, and replaced.
    /* Test 2 */          //will be found, and replaced.

Methods of RegExp and String, matchAll(regexp) is a “newer, improved” variant of str.match . It's used mainly to search for all matches with all� Python string method find() determines if string str occurs in string, or in a substring of string if starting index beg and ending index end are given. Syntax str.find(str, beg=0, end=len(string)) Parameters. str − This specifies the string to be searched. beg − This is the starting index, by default its 0.

Sounds like you've got the solution to your basic question from comments, but let's take a look at your regex so you can understand what the problem was.

Your regex:

\*\s[Test]\s\d+\*

It's looking good for the most part. You've escaped the * by adding \ in front. You're using the \s to match the space. That'll match any whitespace mind you, a tab or an enter, or whatever. If you just wanted space which it looks like you do, you can just put a space there(like this: /* Test */" ).

The main thing you've got wrong is [Test]. This is what's called a character class or a character set. This will match T or e or s or t. Just one of them. Not "Test". When you removed the character class brackets, you're left with "Test" which will match itself exactly. Character classes can be really useful though if you want to match something specific. If we want to match 1, 2, 3, 4, and T and c, or whatever, we could do this [1234Tc].

If you want it one or more times, [1234Tc]+
If you want it zero or more times, [1234Tc]*
If you want it to match between 2 and 5 times, [1234Tc]{2,5}
If you want it to match 4 times, [1234Tc]{4}

That last one would have worked for your character class. [Test]{4} would have matched your test. That said, it would have also matched "esTt"

Anyways, hopefully that's given you a better idea of what was going on there. It'll eventually click once you've learned all the rules. Happy regexing

Find Substring within a string that begins and ends with paranthesis , Find Substring within a string that begins and ends with paranthesis. Comments. Post Posting GuidelinesFormatting. - Now. Top Regular Expressions. str.find() returns the lowest index in the string where the substring sub is found within the slice s[start:end]. It returns -1 if the sub is not found. start and end are optional arguments. The…

with open(fname, 'rw') as f:
    for line_term in f:
        line = line_term.rstrip('\n')
        if re.match('.*\/\* Test \d+ \*\/.*', line):
            print line;

Regular Expression Syntax Reference, Regular expression is to express a characteristic in a string, and then to match another string with the characteristic. For example, pattern "ab+" means "one 'a'� I don’t work a lot with RegEx but when I do, I use tools like PowerRegex from Sapien, RegExr,the technet help forabout_Regular_Expressionsor RegExlib.com. And to be honest, most of the time I’m trying to avoid it…trying to find a solution the “PowerShell Way” before trying with Regex… Problem

How to find a matching substring using regular expression in C#?, Our string is − string str = My make ; Use the following regular expression to find the substring “make” @\bmake\b Here is the complete code −� I've got a data set where a majority of the Address fields contains "ATTN: firstname lastname ### Address", see below, and I'm trying to only pull out the actual street address and not any of the ATTN: or names before the actual address. It seems like I'll need a RegEx/Formula Tool to help accompl

Lua string.find, Find the first match of the regular expression "pattern" in "str", starting at position " index". The starting position (index) is optional, and defaults to 1 (the start of the�

Python String find(), Regex in Python to put spaces between words starting with capital letters � Python Regex to extract maximum numeric value from a string � Find�

Comments
  • Change re.match to re.search and remove square brackets from the regex pattern.
  • and add \s after \d+
  • Thank you for your quick response. I tried re.search('/*\sTest\s\d+*/', line), still no print out. Any more suggestions? Thank you very much.
  • 1, change re.match to re.search; 2, remove square brackets; 3,add \s after \d+. Now it works. Great, Thank you very much for both of you. Thanks.
  • You're not escaping the * in that second one.