string contains and special characters

Discussion in 'Python' started by loial, Oct 9, 2012.

  1. loial

    loial Guest

    I am trying to match a string that containing the "<" and ">" characters, using the string contains function, but it never seems to find the lines containing the string

    e.g if mystring.contains("<TAG>") :

    Do I need to escape the characters...and if so how?
     
    loial, Oct 9, 2012
    #1
    1. Advertising

  2. loial

    Agon Hajdari Guest

    On 10/09/2012 04:02 PM, loial wrote:
    > I am trying to match a string that containing the "<" and ">" characters, using the string contains function, but it never seems to find the lines containing the string
    >
    > e.g if mystring.contains("<TAG>") :
    >
    > Do I need to escape the characters...and if so how?
    >

    if '<TAG>' in yourstring:
    # your code

    --
    Agon Hajdari
     
    Agon Hajdari, Oct 9, 2012
    #2
    1. Advertising

  3. loial

    Dave Angel Guest

    On 10/09/2012 10:02 AM, loial wrote:
    > I am trying to match a string that containing the "<" and ">" characters, using the string contains function, but it never seems to find the lines containing the string
    >
    > e.g if mystring.contains("<TAG>") :
    >
    > Do I need to escape the characters...and if so how?
    >


    What language are you trying to use, and what version of that language?
    In all the Python versions I know of, that line would simply be a syntax
    error. There's no "contains" method in the str class, use the 'in' keyword.

    if "<TAG>' in mystring:

    No need to escape any ASCII characters except backslash.

    --

    DaveA
     
    Dave Angel, Oct 9, 2012
    #3
  4. loial

    Jerry Hill Guest

    On Tue, Oct 9, 2012 at 10:02 AM, loial <> wrote:
    > I am trying to match a string that containing the "<" and ">" characters, using the string contains function, but it never seems to find the lines containing the string
    >
    > e.g if mystring.contains("<TAG>") :
    >
    > Do I need to escape the characters...and if so how?


    Strings don't have a contains() method. Assuming that mystring is
    actually a string, you should be getting a very specific error,
    telling you exactly what's wrong with your code (something like
    AttributeError: 'str' object has no attribute 'contains').

    If that isn't what you're seeing, you'll need to provide the full and
    complete text of the error you are getting, and preferably enough of
    your code that we can reproduce the issue and help you solve it.

    --
    Jerry
     
    Jerry Hill, Oct 9, 2012
    #4
  5. On 09/10/2012 15:23, Dave Angel wrote:
    > On 10/09/2012 10:02 AM, loial wrote:
    >> I am trying to match a string that containing the "<" and ">" characters, using the string contains function, but it never seems to find the lines containing the string
    >>
    >> e.g if mystring.contains("<TAG>") :
    >>
    >> Do I need to escape the characters...and if so how?
    >>

    >
    > What language are you trying to use, and what version of that language?
    > In all the Python versions I know of, that line would simply be a syntax
    > error. There's no "contains" method in the str class, use the 'in' keyword.


    I see you've already corrected youself :)

    >
    > if "<TAG>' in mystring:
    >
    > No need to escape any ASCII characters except backslash.
    >


    No need to escape anything if raw strings are used.

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Oct 9, 2012
    #5
  6. Am 09.10.2012 16:02, schrieb loial:
    > I am trying to match a string that containing the "<" and ">"
    > characters, using the string contains function, but it never seems to
    > find the lines containing the string
    >
    > e.g if mystring.contains("<TAG>") :


    I can't locate a 'contains' function anywhere, what type is 'mystring'?


    > Do I need to escape the characters...and if so how?


    Maybe. Please provide some example code that unexpectedly fails.


    Uli
     
    Ulrich Eckhardt, Oct 9, 2012
    #6
  7. loial

    loial Guest

    On Tuesday, 9 October 2012 15:19:33 UTC+1, Agon Hajdari wrote:
    > On 10/09/2012 04:02 PM, loial wrote: > I am trying to match a string thatcontaining the "<" and ">" characters, using the string contains function,but it never seems to find the lines containing the string > > e.g if mystring.contains("<TAG>") : > > Do I need to escape the characters...and if sohow? > if '<TAG>' in yourstring: # your code -- Agon Hajdari


    That worked...thanks
     
    loial, Oct 9, 2012
    #7
  8. loial

    loial Guest

    On Tuesday, 9 October 2012 15:19:33 UTC+1, Agon Hajdari wrote:
    > On 10/09/2012 04:02 PM, loial wrote: > I am trying to match a string thatcontaining the "<" and ">" characters, using the string contains function,but it never seems to find the lines containing the string > > e.g if mystring.contains("<TAG>") : > > Do I need to escape the characters...and if sohow? > if '<TAG>' in yourstring: # your code -- Agon Hajdari


    That worked...thanks
     
    loial, Oct 9, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    3
    Views:
    33,298
    Stefan Mueller
    Jul 23, 2006
  2. Replies:
    2
    Views:
    1,138
    Ingo Menger
    May 31, 2007
  3. rvino
    Replies:
    0
    Views:
    4,718
    rvino
    Aug 14, 2007
  4. Vijay Pandey

    Data returned by WEB Service contains special characters

    Vijay Pandey, Jun 12, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    211
    Vijay Pandey
    Jun 12, 2006
  5. Peng Yu
    Replies:
    3
    Views:
    222
    C.DeRykus
    Jul 13, 2010
Loading...

Share This Page