#Regex for formatting numbers

1 messages · Page 1 of 1 (latest)

tiny bone
#

I want my code to read something like $33.00 and be able to match it. Also if the user inputs "nothing" it returns 0.00.

"(^$[\\d+]\\.[\\d+])"

this is what I have atm.

pliant rapidsBOT
#

<@&987246399047479336> please have a look, thanks.

pliant rapidsBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

tiny bone
#

actually, is it even possible to handle something like converting the word "nothing" to 0 within a regex?

tropic fox
#

this regex looks good to me "(^$[\\d+]\\.[\\d+])"

tiny bone
#

whenever I test it with a random example it gives me an error

tropic fox
tiny bone
#

other parts as well?

tropic fox
#

y

tiny bone
#

ok

#
public class MembershipFileReader {
    public static void main(String[] args) throws FileNotFoundException {
        String filename;
        double revenue;

        File file = new File("C:\\Users\\myFile\\exampleinput.txt");

        Scanner scan = new Scanner(file);

        String regex = "([a-zA-Z-]+) ([a-zA-Z-]+) has ([\\d]*) [vV]isits left and paid ([%.2f%n]+)" +
" and [s]he joined on (\b(?:Jan(\\w*)?|Feb(\\w*)?|Mar(\\w*)?|Apr(\\w*)?" +
"|May|Jun(\\w*)?|Jul(\\w*)?|Aug(\\w*)?|Sep(\\w*)?|Oct(\\w*)?|(Nov|Dec)(\\w*)?)), " +
"(?:\\d|[12]\\d|3[01]) (\\d{4})";

        String regexTest = "(^$[\\d+]\\.[\\d+])";

        Pattern p = Pattern.compile(regexTest);
        Matcher m = p.matcher("33.10");
        if(m.matches()){
            System.out.println(m.group(0));
        } else {
            System.out.println("Error");
        }
#

the sample input I need to work with is
Alistair Baker-Smith has 7 visits left and paid $123.21 and he joined on November 22, 2022

#

well this is just 1 entry

tropic fox
tiny bone
#

uhh

#

i just played around with it and googled a lot lol

tropic fox
#

I don't know to much regex tho, so I can't help.

tiny bone
#

im debating on switching my methods to just go with .split

tropic fox
#
    public static void main(String[] args) throws FileNotFoundException {
        String filename;
        double revenue;

        File file = new File("C:\\Users\\myFile\\exampleinput.txt");

        Scanner scan = new Scanner(file);
        String[] arr= scan.split(" ");
        int amount = 0;
        for (String word: arr){
           if (word.equals('$33.00")){
              amount=Integer.parseInt(word.substring(word.
            }
      sysout(amount);
pliant rapidsBOT
# tropic fox ```public class MembershipFileReader { public static void main(String[] args...

Detected code, here are some useful tools:

Formatted code
public class MembershipFileReader {
  public static void main(String[] args) throws FileNotFoundException {
    String filename;
    double revenue;
    File file = new File("C:\\Users\\myFile\\exampleinput.txt");
    Scanner scan = new Scanner(file);
    String[] arr = scan.split(" ");
    int amount = 0;
    for (String word : arr) {
      if (word.equals('$33.00")) {
        amount = Integer.parseInt(word.substring(word.}
      sysout(amount);
tropic fox
#

also dont use scanner to read file. It's a good practice to use the BufferedReader class

tiny bone
#

so I would apply this if statement for all 5 key info? like first name, last name, revenue, remaining visits, and join date

tropic fox
#
br.nextLine()
tropic fox
tiny bone
#

well no that's just one of the 5 info i need

tropic fox
#

well the name is always the first word of the sentence ; so it's the first index of the array

#

last name is the second word ; so the second idx of the arr

tiny bone
#

ye i got name visits down

#

just need to figure out the date which is the hardest

tropic fox
#

u can use the regex to match the dates and the revenue

tiny bone
#

oh

tropic fox
#

u good ?

#

it's a school project ?

#

?

tiny bone
#

ye

tropic fox
#

hope this helps, hope u find some1 better

tiny bone
#

nah it helped a lot, thanks 🙂

slow silo
tiny bone
#

(^\\$\\d+\.\\d\\d)

#

i tried with this and it worked