Results 1 to 13 of 13
  1. #1
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Question differens between regex and substring

    well we can use pattern and matcher to find some text from a given text.cant we do same thing by using String classes substring method?

    or what is the difference? can someone explain me?:confused:

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Pattern and Matcher can be used to find the position of a substring that matches some given regex description.

    The substring() method returns the substring between two given positions.
    Last edited by pbrockway2; 04-11-2009 at 03:16 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    There is a closer correspondence between the Matcher method find() and the string method indexOf().

    Both find the position of one string within another. But find() requires the string being sought to be described using a regex while indexOf() requires merely the string to be found.

  4. #4
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    cant we use "public int indexOf(String str, int fromIndex)" to find index of a regular expression?

    and is there any disadvantages of using String class methods over regex class?

    specially if i want to search for regular expression and then replace found expressions with a pre given string?

  5. #5
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    not relevant to this post

    if i want to count number of charactors in a line should this cound include number of spaces between words?

    ex: charactor count of "I Aruna" is 7 or 6?

    thanks

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by aruna1 View Post
    cant we use "public int indexOf(String str, int fromIndex)" to find index of a regular expression?
    No. As I said the str argument of indexOf() specifies the literal string to be found - it does not specify a regular expression.

    Quote Originally Posted by aruna1 View Post
    and is there any disadvantages of using String class methods over regex class?
    Many of the String methods - like indexOf() - work on strings specified literally not as regular expressions. This could be a disadvantage if you are working with regular expressions...

    Quote Originally Posted by aruna1 View Post
    specially if i want to search for regular expression and then replace found expressions with a pre given string?
    For replacing substrings (not the subject of your original post!), the String class provides methods of two flavours:

    replace() replaces one literally expressed string with another.

    replaceAll() replaces all instances that match a given regular expression with a string that is given literally.
    Last edited by pbrockway2; 04-11-2009 at 03:37 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by aruna1 View Post
    not relevant to this post

    if i want to count number of charactors in a line should this cound include number of spaces between words?

    ex: charactor count of "I Aruna" is 7 or 6?

    thanks
    That would depend on how long you consider a piece of string to be.

    Seriously, doesn't this depend on the circumstances? (either the practical requirements of the code, or whatever the documention requires.) If you want to know how many nonspace characters there are (or how many nonspace, non punctuation characters or any other requirement), count them. If you want to know the length of the string, determine that.

  8. #8
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    No. As I said the str argument of indexOf() specifies the literal string to be found - it does not specify a regular expression.



    Many of the String methods - like indexOf() - work on strings specified literally not as regular expressions. This could be a disadvantage if you are working with regular expressions...



    For replacing substrings (not the subject of your original post!), the String class provides methods of two flavours:

    replaces one literally expressed string with another.

    replaces all instances that match a given regular expression with a string that is given literally.
    i guess i still dont see the different between a regular expression and a substring? i mean arent they same? isn't regular expression is just a string? lets say there is a line "I'm aruna rubasinghe from sri lanka" then i can use string "ru" as a regular expression in pattern class or as substring in String class to find occurences of "ru" in given line?:confused::confused:
    Last edited by aruna1; 04-11-2009 at 03:46 AM.

  9. #9
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Check out Regular Expressions on wikipedia to understand them (I don't feel like describing them).
    Examples:

    * .at matches any three-character string ending with "at", including "hat", "cat", and "bat".
    * [hc]at matches "hat" and "cat".
    * [^b]at matches all strings matched by at except "bat".
    * ^[hc]at matches "hat" and "cat", but only at the beginning of the string or line.
    * [hc]at$ matches "hat" and "cat", but only at the end of the string or line.
    when you use indexOf, it will give you the index of "ru" and only "ru"

  10. #10
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Mr.Beans View Post
    Check out ] on wikipedia to understand them (I don't feel like describing them).


    when you use indexOf, it will give you the index of "ru" and only "ru"
    can you please post a simple code where i can see uses of above examples?
    thanks

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    > isn't regular expression is just a string?

    No. A regular expression is a way of specifying a string. To use MrB's example [ch]at is a way of specifying a string that could be either "hat" or "cat". In other words a regular expression gives you lots of options.

    The string "cat" on the other hand specifies just one string: the string "cat". (This is what I meant by saying that string specified the target of your string replacement literally.)

    Regular expressions are able to specify a whole range of strings at once because they use certain letters in a special way: []*? and . all have special meanings (and so do others!) Neither 'r' nor 'u' are special - as it turns out - so the string "ru" or the regular expression ru are going to do the same things.

    Regular expressions are not easy. Your best bet would be to master the String methods that don't use them: indexOf(), replace() and the rest. You do this by writing code. (Yes, you write the code! Post if you get stuck.)

    Regular expressions are inescapable. At any rate if you are going to use replaceAll() then you are going to have to use regular expressions. Your best bet here is to recognise that certain characters are "special" and if you find that the likes of replaceAll() does not do what you expect post a question about what's going wrong. (But, to repeat myself, make sure you are happy with the simpler String methods first.)

  12. #12
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    thanks.I'll try

  13. #13
    aruna1 is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    removed the post
    Last edited by aruna1; 04-11-2009 at 05:19 PM. Reason: Solved the problem

Similar Threads

  1. Help with substring
    By hiklior in forum New To Java
    Replies: 12
    Last Post: 06-28-2008, 06:48 AM
  2. How can i get substring
    By 82rathi.angara in forum New To Java
    Replies: 14
    Last Post: 06-27-2008, 04:35 AM
  3. How to use subString method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:44 PM
  4. String substring function
    By ravian in forum New To Java
    Replies: 6
    Last Post: 01-02-2008, 07:35 PM
  5. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •