I'm trying to write a regular expression in Java for this:
"/[A-Z]{6}-[A-Z]{4}-[A-Z]{4}/"
But it is not working. For example
"AASAAA-AAAA-AAAA".matches("/[A-Z]{6}-[A-Z]{4}-[A-Z]{4}/")
returns false.
What is the correct way?
Java != JavaScript, here you don't need to surround regex with / so try with
"AASAAA-AAAA-AAAA".matches("[A-Z]{6}-[A-Z]{4}-[A-Z]{4}")
Otherwise your regex would search for substring which also has / at start and end.
BTW you need to know that matches checks if regex matches entire String, so
"aaa".matches("aa")
is same as
"aaa".matches("^aa$")
which would return false since String couldn't be fully matched by regex.
If you would like to find substrings which would match regex you would need to use
String input = "abcd";
Pattern regex = Pattern.compile("\\w{2}");
Matcher matcher = regex.matcher(input);
while (matcher.find()){//this will try to find single match
System.out.println(matcher.group());
}
Output:
ab
cd
^[A-Z]{6}-[A-Z]{4}-[A-Z]{4}$
It's just you shouldn't put backslashes at the start and the end. Put instead ^ and $.
And ow I didn't see you have used the Javscript's syntax ! Java != Javascript
Related
I am looking for a regular expression that can strip all 'a' characters from the beginning of an input word (comprising only of English alphabet).
How would I do this using an regular expression?
The following look behind based regex fails to do the job:
(?<=a*?)(\w)+
as for input abc the above regular expression would return abc.
Is there a clean way to do this using lookbehinds?
A (brute force-ish) regular expression that does work is using negation:
(?<=a*)([[^a]&&\w])*
which returns the correct answer of bc for an input word abc.
But I was wondering if there could be a more elegant regular expression, say, using the correct quantifier?
Pattern removeWords = Pattern.compile("\\b(?:a)\\b\\s*", Pattern.CASE_INSENSITIVE);
Matcher fix = removeWords.matcher(YourWord);
String fixedString = fix.replaceAll("");
this will remove a from the current string and if you want to remove some other letters
Pattern removeWords = Pattern.compile("\\b(?:a|b|c)\\b\\s*",Pattern.CASE_INSENSITIVE);
you ca do it this way
I think that a regex for this problem is overkill.
You could instead do:
str = str.startsWith("a") ? str.substring(1) : str;
Try with:
(?i)\\ba?(\\w+)\\b
and replace a word with captured group 1.
Code example:
String word = "aWord Another";
word = word.replaceAll("(?i)\\ba?(\\w+)\\b", "$1");
System.out.println(word);
with output:
Word nother
There are much more simpler way to do this, but as you insist on using using lookbehinds, I will give one. The regex will be
(?<=\b)a+(\w*)
Regex Breakdown
(?<=\b) #Find all word boundaries
a+ #Match the character a literally at least once. We have already ensured using word boundary to find those a's only which are starting of word
(\w*) #Find remaining characters
Regex Demo
Java Code
String str = "abc cdavbvhsza aaabcd";
System.out.println(str.replaceAll("(?<=\\b)a+(\\w*)", "$1"));
Ideone Demo
I have such text:
120.65UAH Produkti Kvartal
5*14 14:24
Bal. 16603.52UAH
What I want to do:
If this text contains "5*14", I need to get 16603.52 via one java reg exp.
this
and this
and this
I tried to create conditional regexp like this:
(5*14 ([\d\.*]+)UAH)
(5*14 d{2}:d{2} Bal. ([\d\.*]+))
etc
But no luck, can you please share your th
You can use a regex like this:
(?=5\*14)[\s\S]*?(\d{5}\.\d{2})
Working demo
Update: you even don't need the look ahead, you can just use:
5\*14[\s\S]*?(\d{5}\.\d{2})
(\d*\.\d\d)(?>\w*)$
will match a group on the last set of DDDDD.DD in the line. You will need to take the contents of the first matching group.
If you have 5*14 before the float number you need to get, you can just use
(?s)\\b5\\*14\\b.*?\\b(\\d+\\.\\d+)
See demo. The value will be in Group 1. I also used Java escaping style.
Note that 5\*14 can match in 145*143 that is why I am using word boundaries \b. .*? with (?s) matches any number of any symbols but as few as possible. \d+\.\d+ matches simple float number (irrespective of the number of digits there are in it).
IDEONE demo:
String str = "120.65UAH Produkti Kvartal\n5*14 14:24\nBal. 16603.52UAH";
Pattern ptrn = Pattern.compile("(?s)\\b5\\*14\\b.*?\\b(\\d+\\.\\d+)");
Matcher matcher = ptrn.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Result: 16603.52
I know it's a simple problem but i'm blocked on it : i want to retrieve all strings written in this form :
$F{ETIQX}
Where X is a number. i wrote this regular expression but i'm getting errors :
if (textField.getText().matches("$F{ETIQ\d}")){
System.out.println("matches!!");
}
Any help will be appreciated.
i want to retrieve all strings
Then you shouldn't be using .matches() in the first place. but a Matcher and .find(). .matches() is a misnomer. It will succeed only if the whole input matches the regex (in contradiction with the definiton of regex matching which can occur anywhere in the input).
Also, your regex should be:
"\\$F\\{ETIQ\\d\\}"
(you need to escape backslashes in a Java string)
$, { and } are regex metacharacters; the first is an anchor matching the end of input, the two latter are bounds for a repetition quantifier.
Your code should read:
private static final Pattern PATTERN = Pattern.compile("\\$F\\{ETIQ\\d\\}");
// ...
final Matcher m = PATTERN.matcher(textField.getText());
while (m.find())
// work with m.group()
\$F\{ETIQ\d\}
escape character which have meaning in regex.
$ means end of string
{ means start of a quantifier
} means end of a quantifier
for matching these you must escape them to match them literally.
here is a demo http://regex101.com/r/xT4mR6
In java \ has no meaning and will throuw an error , so we need to escape \ with \.
Regular expression to obtain value from [[text]]. I have tried the regex
"((?<=[[)*(?=]])*)+" the value between [[ ]] is not obtained.
For example, from the string [[text]], we should obtain text.
Pattern pat = Pattern.compile("((?<=\\[[)*(?=\\]])*)");
Matcher matcher = pat.matcher("[[text]]");
String next ="";
while(matcher.find()) {
next = matcher.group(0);
break;
}
System.out.println(next); //next should be text
You need to escape brackets [] when using them as actual characters in a regular expression. And you also need to add something to actually capture what is between the brackets. You can use .* for that or use my approach, if you are sure the value cannot contain a ].
((?<=\[\[)([^\]]*)(?=\]\]))+
There is not even really a need to use lookbacks und lookaheads unless you explictly need to exempt those limiters from the match. This will work just as well:
\[\[([\]]*\]\]
And obviously when you put these into a String, you need to add additional \ to escape the \ for the String...they are just more readable this way.
If you don't wanna get into regex, String.replaceAll can also help you.
String s2 = s.replaceAll("\\[", "").replaceAll("\\]", "");
"(?<=\\[\\[)[^\\]]*"
this should work for you
I'm trying to make a regex all or nothing in the sense that the given word must EXACTLY match the regular expression - if not, a match is not found.
For instance, if my regex is:
^[a-zA-Z][a-zA-Z|0-9|_]*
Then I would want to match:
cat9
cat9_
bob_____
But I would NOT want to match:
cat7-
cat******
rango78&&
I want my regex to be as strict as possible, going for an all or nothing approach. How can I go about doing that?
EDIT: To make my regex absolutely clear, a pattern must start with a letter, followed by any number of numbers, letters, or underscores. Other characters are not permitted. Below is the program in question I am using to test out my regex.
Pattern p = Pattern.compile("^[a-zA-Z][a-zA-Z|0-9|_]*");
Scanner in = new Scanner(System.in);
String result = "";
while(!result.equals("-1")){
result = in.nextLine();
Matcher m = p.matcher(result);
if(m.find())
{
System.out.println(result);
}
}
I think that if you use String.matches(regex), then you will get the effect you are looking for. The documentation says that matches() will return true only if the entire string matches the pattern.
The regex won't match the second example. It's already strict, since * and & are not in the allowed set of characters.
It may match a prefix, but you can avoid this by adding '$' to the end of the regex, which explicitly matches end of input. So try,
^[a-zA-Z][a-zA-Z|0-9|_]*$
This will ensure the match is against the entire input string, and not just a prefix.
Note that \w is the same as [A-Za-z0-9_]. And you need to anchor to the end of the string like so:
Pattern p = Pattern.compile("^[a-zA-Z]\\w*$")