Tango i nga Tau Mai i te Paihere Ma Te Whakamahi I Nga Whakahuahanga Auau Java

E whai ake nei nga tauira e whakaatu ana me pehea te tango tau mai i te aho e whakamahi ana i nga korero i Java.

Ko te taea ki te wehe i nga taura me te tango korero mai i taua mea he tino pukenga kei ia kaiwhakaata. He tino whaihua tenei ka whakamatauhia nga API ka hiahia koe parse a JSON te whakautu XML ranei.

Ko nga tauira e whai ake nei mo te Whakaaturanga Auau Java e arotahi ana ki te tango i nga tau, tau ranei mai i te aho.

Unuhia Nga Nama Katoa mai i te Miro

import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples {
public static void main(String[]args) {
Pattern p = Pattern.compile('\d+');
Matcher m = p.matcher('string1234more567string890');
while(m.find()) {

System.out.println(m.group());
}
} }

Putanga:

1234 567 890

E pā ana:

Tango mai i te Digitth nth mai i te aho

Mena e hiahia ana koe ki te tango i etahi tau noa mai i te aho ka taea e koe te whakarato taupū ki te group() mahi.



Hei tauira, mena kei te pirangi maatau ki te tango noa i te waahanga tuarua o nga mati mai i te aho string1234more567string890, ara 567 ka taea ai e taatau te whakamahi:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples {
private static final Pattern p = Pattern.compile('[^\d]*[\d]+[^\d]+([\d]+)');
public static void main(String[] args) {
// create matcher for pattern p and given string
Matcher m = p.matcher('string1234more567string890');

// if an occurrence if a pattern was found in a given string...
if (m.find()) {

System.out.println(m.group(1)); // second matched digits
}
} }

Putanga:

567

He Whakamarama mo te Tauira [^d]*[d]+[^d]+([d]+)

  • wareware ki tetahi mati-kore
  • wareware ki tetahi mati (nama tuatahi)
  • whakahawea ano i tetahi-kore
  • hopukia te tau tuarua

Tango Tau mai i te Huanga Tūtohu

I a koe e pa ana ki nga tohu XML, HTML ranei, i etahi wa ka hiahiatia te tango i tetahi uara mai i te huanga. Hei tauira, whakaarohia te tohu e whai ake nei

Hei tango tau 9999 ka taea e taatau te whakamahi i te waehere e whai ake nei:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples {
public static void main(String[]args) {
Pattern pattern = Pattern.compile('numFound='([0-9]+)'');
Matcher matcher = pattern.matcher('');

if (matcher.find()) {

System.out.println(matcher.group(1));
}
} }

Putanga:

9999

Unuhia he aho me nga Tohu kei roto i te aho

Ka taea e koe te whakamahi i nga whakahua auau a Java ki te tango i tetahi waahanga o te aho e mau ana te mati me te kiripuaki. Whakaarohia kei a matou tenei aho Sample_data = YOUR SET ADDRESS IS 6B1BC0 TEXT ana kei te pirangi matou ki te tango 6B1BC0 ko tehea 6 pūāhua te roa , Ka taea e taatau te whakamahi:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples {
public static void main (String[] args) {
Pattern p = Pattern.compile('YOUR SET ADDRESS IS\s+([A-Z0-9]{6})');
Matcher n = p.matcher('YOUR SET ADDRESS IS 6B1BC0 TEXT');
if (n.find()) {

System.out.println(n.group(1)); // Prints 123456
}
} }

Putanga:

6B1BC0

Tangohia nga Paa-Uara Matua Me nga Whakaaturanga Auau

Me kii kei i a maatau tetahi aho o tenei whakatakotoranga bookname=testing&bookid=123456&bookprice=123.45 ana e hiahia ana maua ki te tango i te takirua uara-matua bookid=123456 ka whakamahia e matou:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples {
public static void main(String[] args) {
String s = 'bookname=cooking&bookid=123456&bookprice=123.45';
Pattern p = Pattern.compile('(?<=bookid=)\d+');
Matcher m = p.matcher(s);
if (m.find()) {

System.out.println(m.group());
}
} }

Putanga:

123456