Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package RegularEspressions;

import java.util.regex.*; 
 
public class RegFndMultiple { 
  public static void main(String args[]) { 
    Pattern pat = Pattern.compile("^([+-]?[0-9]\\d*|0)$"); 
    Matcher mat = pat.matcher("word 1 2 3 android"); 
 
    while(mat.find()) { 
      System.out.println("numbers found at index " + 
                         mat.start()); 
    } 
  } 
}

should display position of numbers , code that prints the position of integers

What I have tried:

changed code and added new patterns
Posted
Updated 23-Feb-18 4:07am
Comments
Maciej Los 23-Feb-18 7:20am    
As to me your pattern is wrong. I'd check: \d{1,}
Patrice T 23-Feb-18 7:33am    
What is the question?

Your pattern is wrong, it will match only lines containing an integer and nothing else because of ^ and $.
Pattern would be better as
"([+-]?[0-9]\\d*|0)"

In any cases, the pattern is over complicated, I would use
"([+-]?\\d+)"


Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]

[Update]
Try my pattern and your solution with "test 1 23 -45 test"
You may get surprised.
 
Share this answer
 
v3
Comments
four systems 23-Feb-18 10:00am    
cool guys thank you very much
Patrice T 23-Feb-18 10:10am    
You are welcome.
If the solution was helpful and the problem is solved, Accept the solution.
It will close the question and tell everyone that the question is answered and no more help is needed.
package RegularEspressions;

import java.util.regex.*; 
 
public class RegFndMultiple { 
  public static void main(String args[]) { 
    Pattern pat = Pattern.compile("[0-9]"); 
    Matcher mat = pat.matcher("test 1 2 3 test"); 
 
    while(mat.find()) { 
      System.out.println("numbers found at index " + 
                         mat.start()); 
    } 
  } 
}


did this and the result is

numbers found at index 5
numbers found at index 7
numbers found at index 9
 
Share this answer
 
Comments
Patrice T 23-Feb-18 10:12am    
Try your solution with "test 1 23 -45 test"
four systems 23-Feb-18 10:28am    
the result is

numbers found at index 5
numbers found at index 7
numbers found at index 8
numbers found at index 11
numbers found at index 12
Patrice T 23-Feb-18 10:48am    
Is it what you expect?
Because I see 3 integers.
four systems 26-Feb-18 4:45am    
what it does is checks for each number but as it what the requirement is that check any numbers and put word there, thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900