简体   繁体   中英

Matching “-” has completely unexpected results

I have an expression that could be 13.33 , 15.66-17.22 , 17.33-17.66 . I want to be able to figure out if it has a "-" (en-dash) in it because that will change how my code runs. I followed this thread to check for matches in an expression.

My regex to find the en-dash is "-" . My regex expression works online as can be seen here , but fails when used in Java. My code is as follows.

Pattern p = Pattern.compile("-");
Matcher m = p.matcher(refVal);
System.out.println(m.find());
if (m.find()){
    //Do stuff  
}

With the entry, 17.33–17.66 ref , the code prints false .

The expected use cases:

Input: 17.33-17.66 reasdfkljasdfjlkadsf
Output: m.find() should be true

Input: 17.33
Output: m.find() should be false

Input: 2-3 five blah foo
Output: m.find() should be true

The problem is that in your input string the dash is (150 ascii), while the dash in the pattern is - (45 ascii). Reference

If you want to check the presence of em-dash (or any single character), you can just use String.contains , you don't need to use regular expressions for that.

refVal.contains("—")

To make sure you're testing for em-dash , you can use it's Unicode code to check:

refVal.contains("\u2014")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM