Bvisa Manhamba Kubva Tambo Uchishandisa Java Yakajairika Kutaura

Iyi inotevera mienzaniso inoratidza maitiro ekuburitsa manhamba kubva patambo uchishandisa mataurirwo enguva dzose muJava.

Kugona kuyera tambo uye kuburitsa ruzivo kubva pahwo hunyanzvi hunokosha hunofanira kuve nemuedzo wese. Izvi zvinonyanya kubatsira kana uchiedza APIs uye iwe unofanirwa parse JSON kana XML mhinduro.

Iyi inotevera Java Yakajairika Tsananguro mienzaniso inotarisa pakuburitsa manhamba kana manhamba kubva kuString.

Bvisa maNhamba ese kubva kuTambo

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());
}
} }

Kubuda:

1234 567 890

Inoenderana:

Bvisa nth Digit kubva kuTambo

Kana iwe uchida kuburitsa chete mamwe manhamba kubva patambo iwe unogona kupa index kune iyo group() basa.



Semuenzaniso, kana isu taida kungoburitsa yechipiri seti yemadhijiti kubva patambo string1234more567string890, i.e. 567 ipapo tinogona kushandisa:

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
}
} }

Kubuda:

567

Tsananguro yeMuenzaniso [^d]*[d]+[^d]+([d]+)

  • hanya chero ipi isiri yedhijitari
  • rega chero chero digit (yekutanga nhamba)
  • zvakare hanya nechero ipi isiri-digit
  • tora nhamba yechipiri

Bvisa Nhamba kubva kuTag Attribute

Paunenge uchibata neXML kana HTML tag, dzimwe nguva panofanirwa kubviswa kukosha kubva kuchimiro. Semuenzaniso, funga tag inotevera

Kubvisa nhamba 9999 tinogona kushandisa kodhi inotevera:

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));
}
} }

Kubuda:

9999

Bvisa Tambo Iine manhamba uye Hunhu

Unogona kushandisa Java mataurirwo enguva dzose kuti ubvise chikamu cheTambo chine manhamba uye mavara. Ngatitii tambo iyi Sample_data = YOUR SET ADDRESS IS 6B1BC0 TEXT uye isu tinoda kutora 6B1BC0 zvinova 6 mavara akareba , tinogona kushandisa:

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
}
} }

Kubuda:

6B1BC0

Bvisa Kiyi-Kukosha Mapaviri Neyakajairwa Kutaurwa

Ngatifungei tine tambo yeiyi fomati bookname=testing&bookid=123456&bookprice=123.45 uye isu tinoda kutora iyo yakakosha-kukosha vaviri bookid=123456 taizoshandisa:

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());
}
} }

Kubuda:

123456