第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Java正則表達(dá)式匹配UTF-8字符串(無(wú)副本)

Java正則表達(dá)式匹配UTF-8字符串(無(wú)副本)

神不在的星期二 2023-12-13 16:56:52
我正在從 SocketChannel 加載大型 UTF-8 文本,并且需要提取一些值。模式匹配java.util.regex對(duì)此非常有用,但是解碼為 Java 的 UTF-16 withCharBuffer cb = UTF_8.decode(buffer);會(huì)復(fù)制此緩沖區(qū),使用雙倍的空間。有沒(méi)有辦法以 UTF-8 創(chuàng)建 CharBuffer“視圖”,或者以其他方式與字符集進(jìn)行模式匹配?
查看完整描述

1 回答

?
吃雞游戲

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

您可以創(chuàng)建輕量級(jí)CharSequence包裝ByteBuffer,無(wú)需正確的 UTF8 處理即可執(zhí)行簡(jiǎn)單的字節(jié)到字符轉(zhuǎn)換。


只要您的正則表達(dá)式僅包含 Latin1 字符,它就可以在“天真”轉(zhuǎn)換的字符串上工作。


只有與 reg ex 匹配的范圍才需要從 UTF8 正確解碼。


下面的代碼說(shuō)明了這種方法。


import java.io.UnsupportedEncodingException;

import java.nio.ByteBuffer;

import java.nio.CharBuffer;

import java.nio.charset.Charset;

import java.util.regex.Matcher;

import java.util.regex.Pattern;


import org.junit.Test;


import junit.framework.Assert;



public class RegExSnippet {


    private static Charset UTF8 = Charset.forName("UTF8");


    @Test

    public void testByteBufferRegEx() throws UnsupportedEncodingException {


        // this UTF8 byte encoding of test string

        byte[] bytes = ("lkfmd;wmf;qmfqv amwfqwmf;c "

        + "<tag>This is some non ASCII text 'кирилицеский текст'</tag>"

        + "kjnfdlwncdlka-lksnflanvf ").getBytes(UTF8);


        ByteBuffer bb = ByteBuffer.wrap(bytes);


        ByteSeqWrapper bsw = new ByteSeqWrapper(bb);


        // pattern should contain only LATIN1 characters

        Matcher m = Pattern.compile("<tag>(.*)</tag>").matcher(bsw);


        Assert.assertTrue(m.find());


        String body = m.group(1);


        // extracted part is properly decoded as UTF8

        Assert.assertEquals("This is some non ASCII text 'кирилицеский текст'", body);

    }


    public static class ByteSeqWrapper implements CharSequence {


        final ByteBuffer buffer;


        public ByteSeqWrapper(ByteBuffer buf) {

            this.buffer = buf;

        }


        @Override

        public int length() {

            return buffer.remaining();

        }


        @Override

        public char charAt(int index) {

            return (char) (0xFF & buffer.get(index));

        }


        @Override

        public CharSequence subSequence(int start, int end) {

            ByteBuffer bb = buffer.duplicate();

            bb.position(bb.position() + start);

            bb.limit(bb.position() + (end - start));

            return new ByteSeqWrapper(bb);

        }


        @Override

        public String toString() {

            // a little hack to apply proper encoding

            // to a parts extracted by matcher

            CharBuffer cb = UTF8.decode(buffer);

            return cb.toString();

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-12-13
  • 1 回答
  • 0 關(guān)注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)