我有文字,对于exp:{
"OTA_AirLowFareSearchRS": {
"PricedItinCount": 0,
"BrandedOneWayItinCount": 0,
"SimpleOneWayItinCount": 0,
"DepartedItinCount": 0,
"SoldOutItinCount": 0,
"AvailableItinCount": 0,
"Version": null,
"Success": null,
"Warnings": null,
"PricedItineraries": {
"PricedItinerary": [{
"AirItinerary": {
"OriginDestinationOptions": {
"OriginDestinationOption": [{
"FlightSegment": [{
"DepartureDateTime": "2017-10-02T13:00:00",
"ArrivalDateTime": "2017-10-02T18:33:00",
"StopQuantity": 0,
"FlightNumber": 424,
"ResBookDesigCode": "Z",
"ElapsedTime": 213,
"DepartureAirport": {
"LocationCode": "LAX"
},
"ArrivalAirport": {
"LocationCode": "MSP"
},
"MarketingAirline": {
"Code": "SY"
},
"ArrivalTimeZone": {
"GMTOffset": -5.0
},
"TPA_Extensions": {
"ValidatingCarrier": null,
"eTicket": {
"Ind": true
},
"Mileage": {
"Amount": 1536
}
},
"MarriageGrp": "O",
"Equipment": [{
"AirEquipType": "738",
"content": ""
}],
"OnTimePerformance": null,
"OperatingAirline": {
"FlightNumber": 424,
"Code": "SY",
"content": ""
},
"DepartureTimeZone": {
"GMTOffset": -7.0
}
},
{
"DepartureDateTime": "2017-10-02T19:45:00",
"ArrivalDateTime": "2017-10-02T23:35:00",
"StopQuantity": 0,
"FlightNumber": 249,
"ResBookDesigCode": "Z",
"ElapsedTime": 170,
"DepartureAirport": {
"LocationCode": "MSP"
},
"ArrivalAirport": {
"LocationCode": "JFK"
},
"MarketingAirline": {
"Code": "SY"
},
"ArrivalTimeZone": {
"GMTOffset": -4.0
},
"TPA_Extensions": {
"ValidatingCarrier": null,
"eTicket": {
"Ind": true
},
"Mileage": {
"Amount": 1028
}
},
"MarriageGrp": "I",
"Equipment": [{
"AirEquipType": "738",
"content": ""
}],
"OnTimePerformance": null,
"OperatingAirline": {
"FlightNumber": 249,
"Code": "SY",
"content": ""
},
"DepartureTimeZone": {
"GMTOffset": -5.0
}
}
],
"ElapsedTime": 455
},
{
"FlightSegment": [{
"DepartureDateTime": "2017-10-27T07:00:00",
"ArrivalDateTime": "2017-10-27T09:05:00",
"StopQuantity": 0,
"FlightNumber": 240,
"ResBookDesigCode": "Z",
"ElapsedTime": 185,
"DepartureAirport": {
"LocationCode": "JFK"
},
"ArrivalAirport": {
"LocationCode": "MSP"
},
"MarketingAirline": {
"Code": "SY"
},
"ArrivalTimeZone": {
"GMTOffset": -5.0
},
"TPA_Extensions": {
"ValidatingCarrier": null,
"eTicket": {
"Ind": true
},
"Mileage": {
"Amount": 1028
}
},
"MarriageGrp": "O",
"Equipment": [{
"AirEquipType": "738",
"content": ""
}],
"OnTimePerformance": null,
"OperatingAirline": {
"FlightNumber": 240,
"Code": "SY",
"content": ""
},
"DepartureTimeZone": {
"GMTOffset": -4.0
}
},
{
"DepartureDateTime": "2017-10-27T10:05:00",
"ArrivalDateTime": "2017-10-27T12:00:00",
"StopQuantity": 0,
"FlightNumber": 423,
"ResBookDesigCode": "Z",
"ElapsedTime": 235,
"DepartureAirport": {
"LocationCode": "MSP"
},
"ArrivalAirport": {
"LocationCode": "LAX"
},
"MarketingAirline": {
"Code": "SY"
},
"ArrivalTimeZone": {
"GMTOffset": -7.0
},
"TPA_Extensions": {
"ValidatingCarrier": null,
"eTicket": {
"Ind": true
},
"Mileage": {
"Amount": 1536
}
},
"MarriageGrp": "I",
"Equipment": [{
"AirEquipType": "73G",
"content": ""
}],
"OnTimePerformance": null,
"OperatingAirline": {
"FlightNumber": 423,
"Code": "SY",
"content": ""
},
"DepartureTimeZone": {
"GMTOffset": -5.0
}
}
],
"ElapsedTime": 480
}
]
},
"DirectionInd": "Return"
},
"AirItineraryPricingInfo": [{
"FareReturned": true,
"PricingSubSource": "MIP",
"PricingSource": "ADVJR1",
"LastTicketDate": "2017-09-29T00:00:00",
"PTC_FareBreakdowns": {
"PTC_FareBreakdown": [{
"FareBasisCodes": {
"FareBasisCode": [{
"BookingCode": "Z",
"DepartureAirportCode": "LAX",
"AvailabilityBreak": false,
"ArrivalAirportCode": "MSP",
"content": "ZT"
},
{
"BookingCode": "Z",
"DepartureAirportCode": "MSP",
"AvailabilityBreak": true,
"ArrivalAirportCode": "JFK",
"content": "ZT"
},
{
"BookingCode": "Z",
"DepartureAirportCode": "JFK",
"AvailabilityBreak": false,
"ArrivalAirportCode": "MSP",
"content": "ZT14"
},
{
"BookingCode": "Z",
"DepartureAirportCode": "MSP",
"AvailabilityBreak": true,
"ArrivalAirportCode": "LAX",
"content": "ZT14"
}
]
},
"PassengerTypeQuantity": {
"Quantity": 2,
"Code": "ADT",
"Changeable": false
},
"PassengerFare": {
"FareConstruction": {
"CurrencyCode": "USD",
"DecimalPlaces": 2,
"Amount": 229.77
},
"TotalFare": {
"CurrencyCode": "BRL",
"DecimalPlaces": 0,
"Amount": 934.28
},
"Taxes": {
"TotalFare": null,
"Tax": [{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "US1",
"Amount": 55.02,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "ZP",
"Amount": 13.09,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "ZP",
"Amount": 13.09,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "ZP",
"Amount": 13.09,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "ZP",
"Amount": 13.09,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "AY",
"Amount": 17.88,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "AY",
"Amount": 17.88,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "XF",
"Amount": 14.36,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "XF",
"Amount": 14.36,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "XF",
"Amount": 14.36,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "XF",
"Amount": 14.36,
"content": ""
}
]
},
"BaseFare": {
"CurrencyCode": "USD",
"DecimalPlaces": 0,
"Amount": 229.77
},
"EquivFare": {
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"Amount": 733.7
}
},
"Endorsements": {
"NonRefundableIndicator": true
},
"TPA_Extensions": {
"FareCalcLine": {
"Info": "LAX SY X/MSP SY NYC128.37SY X/MSP SY LAX101.40USD229.77END ZPLAXMSPJFKMSP XFLAX4.5MSP4.5JFK4.5MSP4.5"
}
},
"FareInfos": {
"FareInfo": [{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
}
]
}
},
{
"FareBasisCodes": {
"FareBasisCode": [{
"BookingCode": "Z",
"DepartureAirportCode": "LAX",
"AvailabilityBreak": false,
"ArrivalAirportCode": "MSP",
"content": "ZT"
},
{
"BookingCode": "Z",
"DepartureAirportCode": "MSP",
"AvailabilityBreak": true,
"ArrivalAirportCode": "JFK",
"content": "ZT"
},
{
"BookingCode": "Z",
"DepartureAirportCode": "JFK",
"AvailabilityBreak": false,
"ArrivalAirportCode": "MSP",
"content": "ZT14"
},
{
"BookingCode": "Z",
"DepartureAirportCode": "MSP",
"AvailabilityBreak": true,
"ArrivalAirportCode": "LAX",
"content": "ZT14"
}
]
},
"PassengerTypeQuantity": {
"Quantity": 2,
"Code": "ADT",
"Changeable": false
},
"PassengerFare": {
"FareConstruction": {
"CurrencyCode": "USD",
"DecimalPlaces": 2,
"Amount": 229.77
},
"TotalFare": {
"CurrencyCode": "BRL",
"DecimalPlaces": 0,
"Amount": 934.28
},
"Taxes": {
"TotalFare": null,
"Tax": [{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "US1",
"Amount": 55.02,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "ZP",
"Amount": 13.09,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "ZP",
"Amount": 13.09,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "ZP",
"Amount": 13.09,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "ZP",
"Amount": 13.09,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "AY",
"Amount": 17.88,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "AY",
"Amount": 17.88,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "XF",
"Amount": 14.36,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "XF",
"Amount": 14.36,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "XF",
"Amount": 14.36,
"content": ""
},
{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "XF",
"Amount": 14.36,
"content": ""
}
]
},
"BaseFare": {
"CurrencyCode": "USD",
"DecimalPlaces": 0,
"Amount": 229.77
},
"EquivFare": {
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"Amount": 733.7
}
},
"Endorsements": {
"NonRefundableIndicator": true
},
"TPA_Extensions": {
"FareCalcLine": {
"Info": "LAX SY X/MSP SY NYC128.37SY X/MSP SY LAX101.40USD229.77END ZPLAXMSPJFKMSP XFLAX4.5MSP4.5JFK4.5MSP4.5"
}
},
"FareInfos": {
"FareInfo": [{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
}
]
}
},
{
"FareBasisCodes": {
"FareBasisCode": [{
"BookingCode": "Z",
"DepartureAirportCode": "LAX",
"AvailabilityBreak": false,
"ArrivalAirportCode": "MSP",
"content": "ZTIN"
},
{
"BookingCode": "Z",
"DepartureAirportCode": "MSP",
"AvailabilityBreak": true,
"ArrivalAirportCode": "JFK",
"content": "ZTIN"
},
{
"BookingCode": "Z",
"DepartureAirportCode": "JFK",
"AvailabilityBreak": false,
"ArrivalAirportCode": "MSP",
"content": "ZT14IN"
},
{
"BookingCode": "Z",
"DepartureAirportCode": "MSP",
"AvailabilityBreak": true,
"ArrivalAirportCode": "LAX",
"content": "ZT14IN"
}
]
},
"PassengerTypeQuantity": {
"Quantity": 2,
"Code": "INF",
"Changeable": false
},
"PassengerFare": {
"FareConstruction": {
"CurrencyCode": "USD",
"DecimalPlaces": 2,
"Amount": 0.0
},
"TotalFare": {
"CurrencyCode": "BRL",
"DecimalPlaces": 0,
"Amount": 0.0
},
"Taxes": null,
"BaseFare": {
"CurrencyCode": "USD",
"DecimalPlaces": 0,
"Amount": 0.0
},
"EquivFare": {
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"Amount": 0.0
}
},
"Endorsements": {
"NonRefundableIndicator": true
},
"TPA_Extensions": {
"FareCalcLine": {
"Info": "LAX SY X/MSP SY NYC0.00SY X/MSP SY LAX0.00USD0.00END"
}
},
"FareInfos": {
"FareInfo": [{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
}
]
}
}
]
},
"FareInfos": {
"FareInfo": [{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
},
{
"FareReference": "Z",
"TPA_Extensions": {
"SeatsRemaining": {
"Number": 9,
"BelowMin": false
},
"Cabin": {
"Cabin": "Y"
},
"Meal": {
"Code": "F"
}
}
}
]
},
"TPA_Extensions": {
"DivideInParty": {
"Indicator": false
}
},
"ItinTotalFare": {
"FareConstruction": {
"CurrencyCode": "USD",
"DecimalPlaces": 2,
"Amount": 919.08
},
"TotalFare": {
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"Amount": 3737.12
},
"Taxes": {
"TotalFare": null,
"Tax": [{
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"TaxCode": "TOTALTAX",
"Amount": 802.32,
"content": ""
}]
},
"BaseFare": {
"CurrencyCode": "USD",
"DecimalPlaces": 2,
"Amount": 919.08
},
"EquivFare": {
"CurrencyCode": "BRL",
"DecimalPlaces": 2,
"Amount": 2934.8
}
}
}],
"TicketingInfo": {
"TicketType": "eTicket",
"ValidInterline": "Yes"
},
"TPA_Extensions": {
"TagID": "BargainFinderMaxRQ~8733b3ea-c954-4782-8e75-0332238e6721~1",
"ValidatingCarrier": [{
"Default": null
}]
},
"SequenceNumber": 1
}]
}
},
"Links": [{
"rel": "self",
"": ""
},
{
"rel": "linkTemplate",
"": ""
},
{
"rel": "tagLookupLinkTemplate",
"": ""
},
{
"rel": "paginatedRequestLinkTemplate",
"": ""
}
]
}
我需要将所有带有字符数4(或其他数字)的单词替换为“Test numbers test count gggg aaaaaa
”。
最简单的方法是什么?
现在我尝试做了一些事情,但它无法正常工作:
SUPER
答案 0 :(得分:3)
str.replaceAll("\\b\\w{4}\\b", "SUPER");
应该有用,\\w
表示字符; \\b
表示字边界
答案 1 :(得分:2)
您可以使用此模式:\b\w{4}\b
一个在开头和结尾都有单词边界的字母组
public static String rplcWordWithSize(int size, String sentence) {
return sentence.replaceAll("\\b\\w{" + size + "}\\b", "SUPER");
}
使用示例:
public static void main(String argv[]) {
String str = "Test numbers test count gggg aaaaaa";
System.out.println(rplcWordWithSize(3, str)); //Test numbers test count gggg aaaaaa
System.out.println(rplcWordWithSize(4, str)); //SUPER numbers SUPER count SUPER aaaaaa
System.out.println(rplcWordWithSize(5, str)); //Test numbers test SUPER gggg aaaaaa
}
答案 2 :(得分:1)
请注意[aA-zZ]+
matches more than just letters,因为A-z
范围与英文字母旁边的[, \, ], ^, _, `
匹配。
如果您不希望更换"单词"例如1234
或wrd5
,只想替换自然语言非复合词,请使用以下两种解决方案之一。
这个可识别Unicode,\p{L}
匹配任何Unicode字母和\b
(字边界)"支持"由于Pattern.UNICODE_CHARACTER_CLASS
修饰符嵌入标志(?U)
:
s = s.replaceAll("(?U)\\b\\p{L}{4}\\b", "SUPER");
或者,如果您只打算使用ASCII:
s = s.replaceAll("\\b[a-zA-Z]{4}\\b", "SUPER");
请参阅online Java demo:
System.out.println("Test numbers test count gggg aaaaaa".replaceAll("\\b[a-zA-Z]{4}\\b", "SUPER"));
// => SUPER numbers SUPER count SUPER aaaaaa
System.out.println("Маша ела кашу".replaceAll("(?U)\\b\\p{L}{4}\\b", "SUPER"));
// => SUPER ела SUPER
答案 3 :(得分:0)
试试这个:
Matcher m = p.matcher(myText);
String word = myText.substring(m.start(), m.end());
String[] words = word.Split(" ");
String newword = "";
for(String w : words){
if(w.length == myWord.length){
newword += "SUPER ";
}
else{
newword += w + " ";
}
}
Console.println(newword);
这是否直接来自短信,因此可能会出现一些小错误。