You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Stephan van Hugten (JIRA)" <ji...@apache.org> on 2010/04/16 15:58:28 UTC

[jira] Created: (LANG-619) String prefixing/suffixing

String prefixing/suffixing
--------------------------

                 Key: LANG-619
                 URL: https://issues.apache.org/jira/browse/LANG-619
             Project: Commons Lang
          Issue Type: New Feature
          Components: lang.*
    Affects Versions: 2.5
            Reporter: Stephan van Hugten


I had a use case that I wanted to search through a string and, when I found a match, prefix or suffix (or both) the matching term, i.e. to apply a highlighting class to a matching keyword in a search.
I noticed that this cannot be done by the StringUtils framework, so me and a colleague made this code and would like to donate it to the StringUtils project. In the attachment I included the class with the extensions plus a Unit test testing all the usecases. I hope you like it.

There are 3 methods in the attached class:

prefix: searches through a string and prefixes a match
suffix: searches through a string and suffixes a match
around: does both, prefix or suffix may be null. 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] Commented: (LANG-619) String prefixing/suffixing

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-619?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12857863#action_12857863 ] 

Sebb commented on LANG-619:
---------------------------

This is starting to get off-topic for a JIRA issue. 

Try:

Assert.assertEquals(expected, source.replaceAll("(?i)(cOoL)", this.prefix + "$1" + this.suffix));

The : creates a non-capturing group, which is why there is no group 1 in your example.


> String prefixing/suffixing
> --------------------------
>
>                 Key: LANG-619
>                 URL: https://issues.apache.org/jira/browse/LANG-619
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>    Affects Versions: 2.5
>            Reporter: Stephan van Hugten
>         Attachments: StringExtendedUtils.zip
>
>
> I had a use case that I wanted to search through a string and, when I found a match, prefix or suffix (or both) the matching term, i.e. to apply a highlighting class to a matching keyword in a search.
> I noticed that this cannot be done by the StringUtils framework, so me and a colleague made this code and would like to donate it to the StringUtils project. In the attachment I included the class with the extensions plus a Unit test testing all the usecases. I hope you like it.
> There are 3 methods in the attached class:
> prefix: searches through a string and prefixes a match
> suffix: searches through a string and suffixes a match
> around: does both, prefix or suffix may be null. 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] Commented: (LANG-619) String prefixing/suffixing

Posted by "Stephan van Hugten (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-619?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12857847#action_12857847 ] 

Stephan van Hugten commented on LANG-619:
-----------------------------------------

How would this work for case insensitive searches?

Right now this fails:


String source = "This is cool, oh so supercool";
String expected = "This is SUPERcoolio, oh so superSUPERcoolio";

Assert.assertEquals(expected, source.replaceAll("(?i:cOoL)", this.prefix + "$1" + this.suffix));

while this works:
Assert.assertEquals(expected, source.replaceAll("(cool)", this.prefix + "$1" + this.suffix));

java.lang.IndexOutOfBoundsException: No group 1
	at java.util.regex.Matcher.group(Matcher.java:470)
	at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
	at java.util.regex.Matcher.replaceAll(Matcher.java:813)
	at java.lang.String.replaceAll(String.java:2189)

> String prefixing/suffixing
> --------------------------
>
>                 Key: LANG-619
>                 URL: https://issues.apache.org/jira/browse/LANG-619
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>    Affects Versions: 2.5
>            Reporter: Stephan van Hugten
>         Attachments: StringExtendedUtils.zip
>
>
> I had a use case that I wanted to search through a string and, when I found a match, prefix or suffix (or both) the matching term, i.e. to apply a highlighting class to a matching keyword in a search.
> I noticed that this cannot be done by the StringUtils framework, so me and a colleague made this code and would like to donate it to the StringUtils project. In the attachment I included the class with the extensions plus a Unit test testing all the usecases. I hope you like it.
> There are 3 methods in the attached class:
> prefix: searches through a string and prefixes a match
> suffix: searches through a string and suffixes a match
> around: does both, prefix or suffix may be null. 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] Closed: (LANG-619) String prefixing/suffixing

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-619?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell closed LANG-619.
------------------------------

    Resolution: Won't Fix

Closing as a wontfix.

I think this is complex enough that it's worth using regex's instead of code. I strongly recommend Friedl's Mastering Regular Expressions book - the most valuable computer book I've ever read.

> String prefixing/suffixing
> --------------------------
>
>                 Key: LANG-619
>                 URL: https://issues.apache.org/jira/browse/LANG-619
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>    Affects Versions: 2.5
>            Reporter: Stephan van Hugten
>         Attachments: StringExtendedUtils.zip
>
>
> I had a use case that I wanted to search through a string and, when I found a match, prefix or suffix (or both) the matching term, i.e. to apply a highlighting class to a matching keyword in a search.
> I noticed that this cannot be done by the StringUtils framework, so me and a colleague made this code and would like to donate it to the StringUtils project. In the attachment I included the class with the extensions plus a Unit test testing all the usecases. I hope you like it.
> There are 3 methods in the attached class:
> prefix: searches through a string and prefixes a match
> suffix: searches through a string and suffixes a match
> around: does both, prefix or suffix may be null. 

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (LANG-619) String prefixing/suffixing

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-619?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12857825#action_12857825 ] 

Sebb commented on LANG-619:
---------------------------

Can't you use java.lang.String.replaceAll("(match)", "prefix$1suffix") for this?

> String prefixing/suffixing
> --------------------------
>
>                 Key: LANG-619
>                 URL: https://issues.apache.org/jira/browse/LANG-619
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>    Affects Versions: 2.5
>            Reporter: Stephan van Hugten
>         Attachments: StringExtendedUtils.zip
>
>
> I had a use case that I wanted to search through a string and, when I found a match, prefix or suffix (or both) the matching term, i.e. to apply a highlighting class to a matching keyword in a search.
> I noticed that this cannot be done by the StringUtils framework, so me and a colleague made this code and would like to donate it to the StringUtils project. In the attachment I included the class with the extensions plus a Unit test testing all the usecases. I hope you like it.
> There are 3 methods in the attached class:
> prefix: searches through a string and prefixes a match
> suffix: searches through a string and suffixes a match
> around: does both, prefix or suffix may be null. 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] Commented: (LANG-619) String prefixing/suffixing

Posted by "Stephan van Hugten (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-619?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12858400#action_12858400 ] 

Stephan van Hugten commented on LANG-619:
-----------------------------------------

I agree, but my lack of knowledge of regexes was the reason to opt for my approach. I would like to conclude that these convenience methods are of course null-safe and were useful for our project. You may decide what to do with it in your project.

> String prefixing/suffixing
> --------------------------
>
>                 Key: LANG-619
>                 URL: https://issues.apache.org/jira/browse/LANG-619
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>    Affects Versions: 2.5
>            Reporter: Stephan van Hugten
>         Attachments: StringExtendedUtils.zip
>
>
> I had a use case that I wanted to search through a string and, when I found a match, prefix or suffix (or both) the matching term, i.e. to apply a highlighting class to a matching keyword in a search.
> I noticed that this cannot be done by the StringUtils framework, so me and a colleague made this code and would like to donate it to the StringUtils project. In the attachment I included the class with the extensions plus a Unit test testing all the usecases. I hope you like it.
> There are 3 methods in the attached class:
> prefix: searches through a string and prefixes a match
> suffix: searches through a string and suffixes a match
> around: does both, prefix or suffix may be null. 

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (LANG-619) String prefixing/suffixing

Posted by "Stephan van Hugten (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-619?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Stephan van Hugten updated LANG-619:
------------------------------------

    Attachment: StringExtendedUtils.zip

> String prefixing/suffixing
> --------------------------
>
>                 Key: LANG-619
>                 URL: https://issues.apache.org/jira/browse/LANG-619
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>    Affects Versions: 2.5
>            Reporter: Stephan van Hugten
>         Attachments: StringExtendedUtils.zip
>
>
> I had a use case that I wanted to search through a string and, when I found a match, prefix or suffix (or both) the matching term, i.e. to apply a highlighting class to a matching keyword in a search.
> I noticed that this cannot be done by the StringUtils framework, so me and a colleague made this code and would like to donate it to the StringUtils project. In the attachment I included the class with the extensions plus a Unit test testing all the usecases. I hope you like it.
> There are 3 methods in the attached class:
> prefix: searches through a string and prefixes a match
> suffix: searches through a string and suffixes a match
> around: does both, prefix or suffix may be null. 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira