How To Use Regex As A Key In Hashmap To Match Given String With Key In Java
Apr 3, 2014
I have a table which contains list of regular expression and its corresponding value.I have to fetch those value and put it HASHMAP where regex as key.I have to compare the each key with the given string(input) and If matches I have to get the corresponding Value for the regex.
View Replies
ADVERTISEMENT
Oct 4, 2014
In a code im writing I was curious as to instead of
contains(".")
I wanted to use
matches(" ") method instead but really having issues with the regex
I have tried //. as well as ^[.] but they give me errors.
I am just trying to match a period but was curious as to what the form was. I read a good amount of materials and seemed simple but getting errors.
View Replies
View Related
Feb 14, 2014
With the code below, I am trying to replace all regex matches for visa cards within a given text file.
My first test was with a text "new3.txt" exclusively containing the visa test card 4111111111111111. My objective was to replace the card with "xxxx-xxxx-xxxx-xxxx". This was successful.
However, when modifying the text file to include other characters and text before and after (ex: " qwerty 4111111111111111 adsf zxcv"), it gives mixed results. Although it successfully validates the match, it replaces the whole text in the file, rather than replacing solely the match.
When trying this search and replace with words (rather than a regex match), it does not have this behavior. What am I missing?
import java.io.*;
import java.util.regex.*;
public class BTest
{
//VISA Test
private final static String PATTERN = "(?s).*4[0-9]{12}(?:[0-9]{3})?.*";
public static void main(String args[])
{
try
[Code]...
View Replies
View Related
Apr 19, 2014
I am trying to match validate user input via regex comparison. But i fail to do so.
The string that will be inserted will have the following pattern: "digit/s " and it can be repeated a random number of times.
The program will extend to characters in the future but for now i am struggling with digits.
i am using a StringBuilder to build the regex string. Or maybe i could use something else but so far i didn't make it.
So far i have this regex
Java Code: "(d+s+)+" mh_sh_highlight_all('java');
The problem is that the newline character will interfere with the last number.
So if i enter
Java Code: "3 4 555" mh_sh_highlight_all('java');
- it will not match. And the regex will only see 3 4.
But if i enter
Java Code: "3 4 555 " mh_sh_highlight_all('java');
- with an extra space at the end it works. Obviously that is not the desired case.
I did find a workaround, but i fear it is somewhat broken
I changed to
Java Code: "(d+s*)+" mh_sh_highlight_all('java');
But in this case the pattern should see 3 4 5 5 5 as the regular expression. Am i correct?
I also tried some other variants but none of them worked:
Java Code: "d+(s+|*)" mh_sh_highlight_all('java');
Or i could do something like
Java Code: "(d+s){" + i + "}" mh_sh_highlight_all('java');
- where i is the number of values i enter.
Am i on the right track here? What am i forgetting?
View Replies
View Related
Sep 9, 2014
How can I convert JSON string to HashMap. My JSON string is like
{
"AvailableDeliveries": {
"500": {
"code": "INOFFICE",
"desc": "In Office",
"id": 500,
[code]....
I looked on other examples which have collection of object but I have this extra top level object "AvailableDeliveries" not sure how to handle that level.
View Replies
View Related
Sep 12, 2014
I am trying to write a regular expression for a text which has both String and number... like abc1234, xyz987, gh1052 etc. And the string usually contains 2 or 3 characters.
What I need is two Strings one containing the text (abc, xyz, gh etc) and other containing number (1234, 987, 1052, etc.). Have written the code below. but doesn't seem to work.
String query = "abc1052"; //Or query = "zyx900";
String regexp = "(.{3})(d*)|(.{2}(d*))";
Pattern pattern = Pattern.compile(regexp);
Matcher match = pattern.matcher(query);
if(match.find()){
[Code] ....
Also tried with regexp = "(.s*)(.d*)" in no avail.
Finally achieved with regexp = "(D*)(d*)";
View Replies
View Related
Jan 31, 2014
I have a regular expression that I created and tested on some regex site, [URL] .... It worked on the test site but I can't get it to work in Java.
The code is:
String s = "MyString.ext_CAL3";
assertTrue("Didn't Match", s.matches("_CAL[0-9]+$"));
This test always fails. I cannot figure out what is wrong with it. In fact, I can't get any match no matter what regex I use.
View Replies
View Related
Apr 24, 2015
Why the following string fails the test below:
@Pattern(regexp = "^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*.(([0-9]{1,3})|" +
"([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$", message = "Not a well-formed email address")
View Replies
View Related
Mar 3, 2015
I'm trying to insert some data of type date into database table using hibernate.i take the input date from an xhtml form as shown below
addEvent.xhtml
Event Date (dd-mon-yy) :
<br/>
<h:inputText id="eventdate" value="#{eventBean.eventDate}" p:required="required"
p:type="date"/>
<p/>
<h:commandButton value="Add Event" actionListener="#{eventBean.addEvent}" />
<p/>
This is my addEvent method in EventBean.java
public void addEvent(ActionEvent evt) {
uname = Util.getUname();
boolean a = EventDAO.add(this);
if ( a) {
message = "Event has been added!";
[Code] ....
While executing this..i get the following error: ORA-01861: literal does not match format string. Could it be due to any mismatch in date format (chrome browser automatically takes date in the format mm-dd-yyyy )? If yes, how do I resolve it? (I'm using Oracle database)
View Replies
View Related
Oct 15, 2014
I have 2 strings
String value ="/abc_12_1/abc234/abc/filename.txt";
String src="abc"
Sring tgt=xyz
Now I have to replace the string in variable value in case of exact match; I am trying to do the following :
if(value.contains(src)
{
value.replaceall(src,tgt);
}
Now the problem here is it replaces all the occurrence of abc in the string value and I get the below output as :
value=""/xyz_12_1/xyz234/xyz/filename.txt";
However my requirement is only in the case the value exactly matches with source the replacement shd happen. I am expecting the output like this :
String value ="/abc_12_1/abc234/xyz/filename.txt";
Also the above code is in a function which will be called multiple times and the values will keep on changing. However the target and source will remain the same always.
View Replies
View Related
Jan 1, 2015
I need a pattern that matches Hello Smith (STL Terminal) (15.0).
View Replies
View Related
Jun 12, 2014
I am trying to match the input string with range of values and getting PatternSyntaxException. below is my code
public static void main(String[] args) {
String regex="01[4-6]";
String code="015";
boolean bool=code.matches(regex);
System.out.println(bool);
}
output: true
But when i try to give range in double digits, it throws exception. What if i have to match a number(as string) between 10 to 25 or like that.
public static void main(String[] args) {
String regex="0[14-16]";
String code="015";
boolean bool=code.matches(regex);
System.out.println(bool);
}
Output:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 5
0[14-16]
^
View Replies
View Related
Feb 18, 2014
I have a large text file of 1 GB size. I need to print the line when a matching word is found in a particular line. Below is the code I am using. But if there are many lines that has the matching word, it's taking lot of time. Any solution to print the lines much faster.
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.contains("xyz")) {
System.out.println(line);
}
}
View Replies
View Related
Mar 18, 2014
I need to create a regexp, that will do the following:
a,a,a,a,c - matches
c,a,a,a,a - matches
a,a,a,a,a - doesn't match
I will be using it in Java. In the place of 'a', can be 'b' - they are equal. Also, in the place there can be any other character. This is what i have came up with:
^(?=^(a|b)).*((a|b),){4}(?!(a|b))|(?!(a|b)).*(((a| b),*){4})$
It fails because it matches the 5 a's. I'm quite new to regexp, so I'm not aware of all the possibilities. It matches the 5 a's, because the first if fails, but the second does not. Maybe there is a simpler way to accomplish this? (Also why are the .* necessary in the middle?)
View Replies
View Related
Dec 21, 2014
I have the following hashMap declared:
private HashMap<String, Car> cars = new HashMap<String, Car>();
How can i get an array of String filled with hashMap keys?
View Replies
View Related
Oct 2, 2014
When learning HashMaps in C++ I had to create the whole algorithm. In the code I created I could simply place a string into the method and it would store the names for me by turning the string into a integer and storing is accordingly. If there was a collision it would grow linearly at that location.
//play with Hash Tables
void getNames(String names) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put(names,22);
}
How can I do this in Java. I read about them and look at examples and they all for the most part look like this.
View Replies
View Related
Aug 20, 2014
I am kind of new to Java. I have a query.. look at it. Have an xml like
<ProductList>
<Product Quality='Good' color='Blue'>
<Details ItemID='1001'/>
</Product>
...
..
</ProductList>
The <Product> tags can run into hundreds or thousands. Quality can have values like Good, Bad, Damaged. Color can also have various values (Blue, Red ..)
ItemID can repeat or can be different. So I want group by ItemID->Color->Good count or BadCount and TotalCount as below.
I want to get a sum like
<ProductList>
<ProductType ItemID="1001">
<Product_Detail CountGood="1" CountBad="2" CountTotal="3" Type="Blue"/>
</ProductType>
...
...
</ProductList>
I think using Hashmaps (Hashmap1 for ItemID, Hashmap2 for color, Hashmap3 for quality) may do the work. But I have to parse the xml number of ItemID's multiply-by number of colors multiply by various colors times for this.
Any better algorithm is there in performance perspective using Java.
View Replies
View Related
Feb 11, 2014
How to implement HashMap put and get methods without using Java Collection framework?
View Replies
View Related
Apr 23, 2014
I'm trying to use regex to find a word containing the substring "cat." I'm not sure why this isn't working?
String match = "^ cat$";
View Replies
View Related
Jun 11, 2014
I would like to test whether the first word in a line is is a valid var name (e.g sequence of letters, digits and "_", it may not start with a digit, if "_" is the first char it must be followed by a letter). I tried this simple code:
String namePattern = "_[a-zA-Z0-9]+";
String text = "__c";
Pattern pattern = Pattern.compile(namePattern);
Matcher matcher = pattern.matcher(text);
"__c" is an illegal var name.
But it returns the string "_c", where it is supposed to return an empty matcher.
where am I wrong here?
View Replies
View Related
Apr 4, 2014
I am trying to add an email validator to my xhtml page, but I get this error:
<f:validateRegEx> Tag Library supports namespace: [URL] ...., but no tag was defined for name: validateRegEx
This is my code:
<h:inputText tabindex="7" styleClass="input" id="email" value="#{user.email}"
required="true" validatorMessage="Invalid email!">
<a4j:support id="emailRenderer" event="onblur"
reRender="emailPanel, errorPanel"
ajaxSingle="true"/>
<f:validateRegEx pattern="[w.-]*[a-zA-Z0-9_]@[w.-]*[a-zA-Z0-9].[a-zA-Z][a-zA-Z.]*(com|net|org|edu)" />
</h:inputText>
<p:message for="email" />
[Code] ....
View Replies
View Related
Jun 13, 2014
I have one column "category" which contain data like
`"Failed extract of third-party root list from auto update cab at: [URL] with error: The data is invalid."`
I need to select url part in between `" < > "` sign of category column.
how to do this usign regex?
View Replies
View Related
Apr 14, 2014
I am in need of regex for alphanumeric (uppercase only) values which will verify string of length 5below
ABCCD - False
AB12C - True
ABC12 - True
12ABC - True
12345 - False
If it contain any lowercase then it should result in false as well.
My regex ^[A-Z0-9]{5}$ is not working for above type of values.
View Replies
View Related
Oct 4, 2014
private static int getStrength(String pw) {
int strength = 0;
if(pw.length() >= 8){
strength++;
[Code] .....
This function doesn't seem to work for me. I believe the issue lies in the special character matching. It seems like it always returns true and adds to the strength. But I only want it to add to strength if at least one the following are in the password: *, -, _, ^, !, %
View Replies
View Related
May 14, 2015
I am using the following regex - [a-zA-Z0-9]{9,18} which means I can use alphabets and numbers with minimum length as 9 and maximum length as 18.It should not take special characters.
It takes values like ADV0098890 etc. but it is also taking ADV0098890[] which is wrong.
How can I prevent that ?
View Replies
View Related
Jan 27, 2014
I have a program in which I am prompting users for integer values to display in a JFrame. I call the method below to load an array with their input:
Java Code:
public String inputAssembly(){
if (!jtfInput.getText().matches("d")){
JOptionPane.showMessageDialog(null, "Input must be of integer value.");
} if (jrbFar.isSelected()){
return jtfInput.getText() + jrbFar.getText();
[Code] ....
Regardless of the input, both messages display (invalid input / got it). I've tried debugging so I know that the values are getting entered and read correct, at least to my knowledge. It is a very simple regular expression, only checking to be sure an integer was entered.
View Replies
View Related