Why isn't this regex matching a string with percentage symbol?
<p>I have a file which has the following input : </p>

<pre><code>xa%1bc
ba%1bc
.
.
</code></pre>

<p>and so on. I want to use <code>match</code> and <code>regex</code> to identify the lines which have <code>a%1b</code>in them. 
I am using </p>

<pre><code> import re
 p1 = re.compile(r'\ba%1b\b', flags=re.I)
 if re.match(p1,linefromfile):
    continue
</code></pre>

<p>It doesnt seem to detect the line with %1. What is the issue? Thanks</p>

<p><code>match</code> only search the pattern at the beginning of the string, if you want to find out if a string contains a pattern, use <code>search</code> instead. Besides you don't need the word boundary, <code>\b</code>:</p>

<blockquote>
  <p>re.search(pattern, string, flags=0) </p>
  
  <p>Scan through string looking for
  the first location where the regular expression pattern produces a
  match, and return a corresponding match object. Return None if no
  position in the string matches the pattern; note that this is
  different from finding a zero-length match at some point in the
  string.</p>
  
  <p>re.match(pattern, string, flags=0) </p>
  
  <p>If zero or more characters at the
  beginning of string match the regular expression pattern, return a
  corresponding match object. Return None if the string does not match
  the pattern; note that this is different from a zero-length match.</p>
</blockquote>

<pre><code>import re
if re.search(r"a%1b", "xa%1bc"):
    print("hello")
# hello
</code></pre>

python regex string match word-boundary