0

I searched lot, but cant get perfect solution for below question.

I have a following code to get string values from one line of string by making regex and creating pattern object and matching in string with Matcher.

public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<String>();
    list.add("product val='321ddfgsagsdf' find way");
    list.add("product val='781ertergdfv' find way");
    list.add("product val='96puil$hjgh&fgh' find way"); //Here special characters in string
    list.add("product val='25errgfgsagsdf' find way");
    list.add("product val='562qweddfgsagsdf' find way");

    for (String string : list) {
        Pattern myPat = Pattern.compile(" val='(.*?)'", 2);
        Matcher m = myPat.matcher(string);
        if (m.find()) {
            System.out.println("Found value: " + m.group(1));
        }       }
}

Here I am able to get values for ref, inside the single quotes.

But, third value is having "$" and "&" in ref value, which I don't want to process it in pattern.

I know it is possible by checking string, if string contains special chars and avoid it. But, I want to make it using regex, as my app is checking very much bigger strings.

Is there any way to avoid this special characters in java regex?

5
  • 1
    What is a "special chars" in your case? Does a space qualify? Puctuation? Commented Sep 2, 2016 at 10:21
  • 2
    Are you sure your code works? You do not even run the matcher. Commented Sep 2, 2016 at 10:22
  • @Lucero, space and Punctuation are not allowed, Commented Sep 2, 2016 at 10:24
  • 2
    Yes, but your regex says val and there is no val in the input. There is ref. Please make sure you post what you really have, or we'll be unable to help quickly and efficiently. If you want to follow your blacklisting approach try Pattern myPat = Pattern.compile(" ref='([^\\s\\p{P}\\p{S}]+)'");. However, as I thought initially, Pattern myPat = Pattern.compile(" ref='(\\w+)'"); should work, too. Commented Sep 2, 2016 at 10:27
  • Isn't the answer below enough? It was the solution in my first removed comment. Commented Sep 2, 2016 at 10:37

1 Answer 1

1

You can restrict the match like this (that would be alphanumeric for instance, but you can also use a character class [0-9a-z] or so):

Pattern myPat = Pattern.compile(" val='(\\w+)'", Pattern.CASE_INSENSITIVE);
Sign up to request clarification or add additional context in comments.

4 Comments

Code with this pattern doesn't compile: http://ideone.com/mCswNx.
@DimaSan That's because the sample is missing a import java.util.regex.*; at the top - it has nothing to do with my answer.
Ok fine, can you pls help me for the following scenario? If I just want to avoid only "$" and "&", but need to allow all other characters.
@Clara Then you could use Use " val='([^$&']+)'" as pattern. This is very basic regex stuff, you should find what you need for instance here: regular-expressions.info - Also, if your question is answered, please accept the answer which helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.