简体   繁体   中英

Using positive look-ahead pattern in java matchig

I have a task in which I need to parse some data and migrate it to another column. And in some cases I need to replace some parts depending on conditions.

I read that java Pattern/Matcher supports look-ahead/look-behind operations(or grouping, I do not know how exactly it is called).

So for example I have such string 100 K and I want to convert it into 100000

I used such pattern: (?i)(?=[0-9]+)\\s*[k]\\b

Matcher m = Pattern.compile("(?i)(?=[0-9]+)\\s*[k]\\b").matcher("100 K");
if(m.find()){
    System.out.println(m.start());
    return;
}
System.out.println("false");

As a result I always receive false . And I expected to see 3 . If I change pattern to (?i)([0-9]+)\\\\s*[k]\\\\b and use exact match( matches ) operation then I see true and if I change pattern to negative look-ahead (?i)(?![0-9]+)\\\\s*[k]\\\\b then find() returns true and start() returns 3 .

The question is does I made a mistake in pattern or I use it in a wrong way?

You do not need look ahead for this. The way you've written it, it is looking for something followed by 0-9 (rather than 0-9 followed by k), and then wants spaces and ak, which can't happen as to match the first part, there must be numbers there - not spaces and ak immediately.

(?i)1(?=[0-9]+)([0-9]+)\s*[k]\b  

or

(?i)([0-9]+)(?=\s*[k])\b

would match, but it doesn't seem to be what you need as you want to replace the " k". You could do this with capturing groups. Your negative look-ahead attempt is actually returning the index you want but in an odd way - it's looking for something that isn't followed by a number, and is space(s) + k. It would not actually require the numbers.

(?i)([0-9]+)(\s*[k])\b   

will catch the " k" in the second group. Then you can use:

m.replaceFirst(m.group(1) + "000")

Keep it simple, All you need to find is digits followed by (K or k)

Regex: \\d{1,}\\s*[Kk]

Matcher m = Pattern.compile("\\d{1,}\\s*[Kk]").matcher("100 K");
if(m.find()){
    System.out.println(m.start());
    return;
}
System.out.println("false");

start() will get you the index of the first character matched

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