My file (rates.txt) contains: Abc defghijk lmn opqr st uvwxyza bc 19
I want to extract only the 19 from the file and store it as an integer.
How do I go about this?
I've tried using a substring but am unsure what method to use in order to make it select only the numerical value.
Thanks
What if you use a regular expression.
Try this (I assumed you only have one integer value):
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tester {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\D*(\\d+)\\D*");
Matcher matcher = pattern.matcher("Abc defghijk lmn opqr st uvwxyza bc 19");
try {
if (matcher.find()) {
String stringDigit = matcher.group(1);
int number = Integer.parseInt(stringDigit);
System.out.println(number);
}
} catch(Exception e) {
}
}
}
The pattern "\D*(\d+)\D*" actually means:
\D*: multiple non-digit characters (including zero amount of character)
\d+: multiple digit characters
So, we are searching some digit characters enclosed by some non-digit characters.
Pattern.compile("\\D*(\\d+)\\D*");
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String text = "xAbc defghijk lmn opqr st uvwxyza bc 19";
Pattern pattern = Pattern.compile("([0-9]+)");
Matcher matcher = pattern.matcher(text);
if(matcher.find()) {
int number = Integer.valueOf(matcher.group(1));
System.out.println("First number: "+number);
}
else {
System.out.println("Not found");
}
}
}
Loop through each line within the file.
To do so you can use Iterator and its method hasNext()
Once you've retrieved a line, split it by the empty spaces as follows:
String str = "";
List< String> splitElements = Arrays.asList(str.split("\s+"));
Afterwards, loop through each element of the list and it'd be a good idea to create a helperMethod in charge of verifying whether an element is number or not. It could be achieved with something like this:
public static boolean isNumeric(String string) {
try
{
int intString = Integer.parseInt(string);
}
catch(NumberFormatException ex)
{
return false;
}
return true;
}
Make notice that it parses just int numbers, create more helper methods and you're done, hope it helps!
Related
I'm creating this method to search the text only for the Service Order number, this information can come anywhere in the text because it is an open field for typing, I only need the first value found if you have more than one service order.
example text:
Please check service order number 1-202012345678 for vehicle repair toyota Corolla red
Could someone help me find the error?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Encontra_Ordem {
private static final Pattern PATTERN = Pattern.compile(".*([1#][-#]\\d{12}).*");
public static String buscaordemnotexto(String texto) {
String valor = "";
Matcher matcher = PATTERN.matcher(texto);
if(matcher.matches() && matcher.groupCount() == 1){
String numerodaordem = matcher.group(1);
valor += numerodaordem;
}
return valor;
}
}
With the given data I'd suggest this solution:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Encontra_Ordem {
private static final Pattern PATTERN = Pattern.compile("\\b\\d-\\d{12}\\b");
public static String buscaordemnotexto(String texto) {
String valor = "";
Matcher matcher = PATTERN.matcher(texto);
if (matcher.find()) {
String numerodaordem = matcher.group();
valor += numerodaordem;
}
return valor;
}
public static void main(String[] args) {
System.out.println(buscaordemnotexto("asda sd asd asd asd sa 2-202012345678 ttttttt 1-202012345678"));
}
}
Output
2-202012345678
Explanation
Here's the breakdown of the RegEx I wrote (click on it to make it more readable):
I also used matcher.find() inside an if block so that the content of matcher.group() will be the first RegEx match.
The find() Method
Put simply, the find() method tries to find the
occurrence of a regex pattern within a given string. If multiple
occurrences are found in the string, then the first call to find()
will jump to the first occurrence. Thereafter, each subsequent call to
the find() method will go to the next matching occurrence, one by one.
Sources
https://regexr.com
https://www.baeldung.com/java-matcher-find-vs-matches
I am writing a program and want the program to not loop and request another search pattern if the search pattern (word) contains any non alpha numeric characters.
I have setup a Boolean word to false and an if statement to change the Boolean to true if the word contains letters or numbers. Then another if statement to allow the program to execute if the Boolean is true.
My logic must be off because it is still executing through the search pattern if I simply enter "/". The search pattern cannot contain any non alpha numeric characters to include spaces. I am trying to use Regex to solve this problem.
Sample problematic output:
Please enter a search pattern: /
Line number 1
this.a 2test/austin
^
Line number 8
ra charity Charityis 4 times a day/a.a-A
^
Here is my applicable code:
while (again) {
boolean found = false;
System.out.printf("%n%s", "Please enter a search pattern: ", "%n");
String wordToSearch = input.next();
if (wordToSearch.equals("EINPUT")) {
System.out.printf("%s", "Bye!");
System.exit(0);
}
Pattern p = Pattern.compile("\\W*");
Matcher m = p.matcher(wordToSearch);
if (m.find())
found = true;
String data;
int lineCount = 1;
if (found = true) {
try (FileInputStream fis =
new FileInputStream(this.inputPath.getPath())) {
File file1 = this.inputPath;
byte[] buffer2 = new byte[fis.available()];
fis.read(buffer2);
data = new String(buffer2);
Scanner in = new Scanner(data).useDelimiter("\\\\|[^a-zA-z0-9]+");
while (in.hasNextLine()) {
String line = in.nextLine();
Pattern pattern = Pattern.compile("\\b" + wordToSearch + "\\b");
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
System.out.println("Line number " + lineCount);
String stringToFile = f.findWords(line, wordToSearch);
System.out.println();
}
lineCount++;
}
}
}
}
Stop reinventing the wheel.
Read this: Apache StringUtils,
Focus on isAlpha,
isAlphanumeric,
and isAlphanumericSpace
One of those is likely to provide the functionality you want.
Create a separate method to call the String you are searching through:
public boolean isAlphanumeric(String str)
{
char[] charArray = str.toCharArray();
for(char c:charArray)
{
if (!Character.isLetterOrDigit(c))
return false;
}
return true;
}
Then, add the following if statement to the above code prior to the second try statement.
if (isAlphanumeric(wordToSearch) == true)
Well since no one posted REGEX one, here you go:
package com.company;
public class Main {
public static void main(String[] args) {
String x = "ABCDEF123456";
String y = "ABC$DEF123456";
isValid(x);
isValid(y);
}
public static void isValid(String s){
if (s.matches("[A-Za-z0-9]*"))
System.out.println("String doesn't contain non alphanumeric characters !");
else
System.out.println("Invalid characters in string !");
}
}
Right now, what's happening is if the search pattern contains non alphanumeric characters, then do the loop. This is because found = true when the non alphanumeric characters are detected.
if(m.find())
found = true;
What it should be:
if(!m.find())
found = true;
It should be checking for the absence of nonalphanumeric characters.
Also, the boolean flag can just be simplified to:
boolean found = !m.find();
You don't need to use the if statement.
I'm trying to validate Utopian ID number using java regex classes, ie Pattern and Matcher.
The following are the conditions which needs to be satisfied,
The string must begin with between 0-3 (inclusive) lowercase alphabets.
Immediately following the letters, there must be a sequence of digits (0-9), The length of this segment must be between 2 and 8, both inclusive.
Immediately following the numbers, there must be atleast 3 uppercase letters.
Following is the code which I've written,
public class Solution{public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int ntc;
String[] str;
try {
ntc = Integer.parseInt(br.readLine());
str = new String[ntc];
for (int i = 0; i < ntc; i++)
str[i] = br.readLine();
for (int i = 0; i < ntc; i++)
if (validate(str[i]))
System.out.println("VALID");
else
System.out.println("INVALID");
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean validate(String str) {
Pattern pr = Pattern.compile("[a-z]{0,3}[0-9]{2,8}[A-Z]{3,}");
Matcher mr = pr.matcher(str);
return mr.find();
}}
The following is the input and its respective o/p
I/P:
3
n761512618TUKEFQROSWNFWFWEQEXKPWYYCRK
rRf99
198VLHJIYVEBODQCQEGYGECOGRMQPE
O/P:
VALID
INVALID
VALID
The first testcase is Invalid as it has nine numbers instead of maximum of eight. However it says Valid.
Is there anything wrong in the Regex pattern which I've written.?
Use start and end anchors in your regex in-order to do an exact string match.
Pattern pr = Pattern.compile("^[a-z]{0,3}[0-9]{2,8}[A-Z]{3,}$");
Without anchors, it would match from the middle of a string also.
Use Matcher.matches() rather than Matcher.find(), in order to match the regexp against the entire string:
private static boolean validate(String str) {
Pattern pr = Pattern.compile("[a-z]{0,3}[0-9]{2,8}[A-Z]{3,}");
Matcher mr = pr.matcher(str);
return mr.matches();
}
Also, since the pattern never changes, I would move it into a constant so that it won't be recompiled every time the method is called:
static final Pattern UTOPIAN_ID_PATTERN =
Pattern.compile("[a-z]{0,3}[0-9]{2,8}[A-Z]{3,}");
private static boolean validate(final String str) {
Matcher mr = UTOPIAN_ID_PATTERN.matcher(str);
return mr.matches();
}
I have a string String myString = "abc,QWAB 123,cdef";.
How to write regex to check whether my string has AB (space)123. I don't want to consider QW. Also number 123 will be one digit or two digit or three digit, Always it will not be three digit.
Please help.
Pattern pat = Pattern.compile( "AB \\d{1,3}" );
^[^,]+,AB \d{1,3},.*$
Try this.This should do it.See demo.
https://regex101.com/r/gX5qF3/8
Try something like:
String myString = "abc,QWAB 123,cdef";
if (myString.matches(".*AB [0-9]{1,3}.*")) {
System.out.println("Yes it mateched");
} else {
System.out.println("Hard luck");
}
This this:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Hello world!
*
*/
public class App {
static String myString = "abc,QWAB 123,cdef";
static String abcPattern = "(AB[\\s]{1}\\d{1,3})";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(App.abcPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(myString);
if (matcher.find()) {
System.out.println(matcher.group());
} else {
System.out.println(" No mach found ");
}
}
}
I have a file directory which could look like either
C:\projects\lab3\test\test.java
or
C:\projects\assignment3\test\test.java
But the "lab3" or "assignment3" can appear anywhere in the directory, it is not a set directory
What i want is to check to see if the directory either contains "lab" or "assignment" and get the number that follows. In this case "3"
This is what i have so far
if(directory.toLowerCase().contains("lab")){
} else if (directory.toLowerCase().contains("assignment")){
}
but i do not know how to check for the char straight after the word?
You could use regex:
Pattern pattern = Pattern.compile(".*(?:lab|assignment)([0-9]).*", Pattern.CASE_INSENSITIVE);
String substr = pattern.matcher(directory).replaceAll("$1");
This gets any single-digit number after the last occurrence of either "lab" or "assignment" in the string.
For this to work, you need to import the following classes:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
This does what you are asking for:
String directory = "C:\\projects\\lab3\\test\\test.java";
if(directory.toLowerCase().contains("lab")){
System.out.println(directory.substring(directory.indexOf("lab")+3, directory.indexOf("lab")+4));
}
The +3 parameter is because of the size of "lab" String. You can do better this way:
String directory = "C:\\projects\\lab3\\test\\test.java";
String myWord = "lab";
if(directory.toLowerCase().contains(myWord)){
System.out.println(directory.substring(directory.indexOf(myWord)+ myWord.length(), directory.indexOf(myWord)+ myWord.length()+1));
}
You can use String#substring to extract the part after the "lab" and then use Integer#parseInt to check it's an integer and convert it.
String partAfterLab = filename.substring(4);
You can use a regular expression like C:\\\\projects\\\\(lab|assignment)([0-9]+)\\\\.* to extract the digit(s) after "lab" or "assignment".
use regex
public static void main(final String[] args) {
final String dir = "C:\\projects\\lab3\\test\\test.java";
final Pattern pattern = Pattern.compile(".*?(lab)(\\d+).*");
final Matcher matcher = pattern.matcher(dir);
if (matcher.find()) {
System.out.println(matcher.group(1)); //contains lab
System.out.println(matcher.group(2)); //contains number
}
}
Here is another approach:
Integer.valueOf(s.replaceFirst(".*(assignment|lab)([0-9]+).*", "$2"));
For example:
String assignment = "C:\\projects\\assignment3\\test\\test.java";
String lab = "C:\\projects\\lab3\\test\\test.java";
String none = "C:\\projects\\test\\test.java";
try {
int assignmentNumber = Integer.valueOf(assignment.replaceFirst(".*(assignment|lab)([0-9]+).*", "$2"));
System.out.println(assignmentNumber);
} catch (NumberFormatException e) {
System.out.println("Can't find neither assignment nor lab");
}
try {
int labNumber = Integer.valueOf(lab.replaceFirst(".*(assignment|lab)([0-9]+).*", "$2"));
System.out.println(labNumber);
} catch (NumberFormatException e) {
System.out.println("Can't find neither assignment nor lab");
}
try {
int noneNumber = Integer.valueOf(none.replaceFirst(".*(assignment|lab)([0-9]+).*", "$2"));
System.out.println(noneNumber);
} catch (NumberFormatException e) {
System.out.println("Can't find neither assignment nor lab");
}
Prints:
3
3
Can't find neither assignment nor lab