I want to check if a string matches the form az^3 where a is any integer.
I've tried the following:
str.matches("\\d* z^3")
str.matches("\\d* z\^3")
str.matches("^(\\d* z^3)$")
str.matches("^(\\d* z\^3)$")
str.matches("\\d* (z^3)")
str.matches("\\d* (z\^3)")
This is driving me crazy. :-(
I've tried every possible regex tutorial and searched for examples and I still can't even come up with a solution.
I'd really appreciate if anyone can help me.
You need to escape the backslash in Java
str.matches("\\d+z\\^3");
Related
This is my regex .+\s*(?=!|<|>|=|LIKE) and it doesnt work.
I want to match everything on the left side to the operators <,>,<=,>=,=,!=,LIKE.
Obviously my regex doesn't do the work so I am wondering if you could help me.
Check out what is wrong on this link: https://regex101.com/r/skRdTr/1
(it matches everything on the left side, including >,<,! if operator is <=,>=,!=)
I found the answer: .+?\s*(?=!|<|>|=|LIKE).
Before y'all jump on me for posting something similar to previous questions asked, yes, there seem to be a number of regex related questions but nothing which seems to help me, or at least that I can see.
I am trying to parse strings in JAVA using PATTERN and MATCHER and am really having no joy. My regular expression seems to match my input string when I use a few of the online regular expression testing websites but Java simply does not match my expression.
My input string is:
"Big apple" title="Little Apple" type="Container" url="http://malcolm.com/testing"
The regular expression I am using to match is ".*" title="(.*)" type="Container" url="(.*)"
Essentially I want to pull out the text within the second and the fourth set of quotes. There will always be 4 sets of quotes with text within and around.
I am coding as follows:
Variable XMLSubstring contains the string above (including the quotes) and is as stated, even when I print it out.
Pattern p = Pattern.compile(".* title=\"(.*)\" type=\"Container\" url=\"(.*)\"");
m = p.matcher(XMLSubstring);
It doesn't appear to be rocket science I'm attempting but I'm pulling my hair out trying to debug the bloody thing.
Is there something wrong with my regex pattern?
Is there something wrong with the code I am using?
Am I simply a moron and should stop coding with immediate effect?
EDIT & UPDATE: I have found the problem. My string had a space at the end of it which was breaking the parser! How silly, and I think based on that, I need to accept the third suggestion of mine and give up programming. Thanks all for your assistance.
Try this,
String str="\"Big apple\" title=\"Little Apple\" type=\"Container\" url=\"http://malcolm.com/testing\"";
Pattern p=Pattern.compile(".* title=\\\".*\\\" type=\\\"Container\\\" url=\\\".*\\\"");
Matcher m=p.matcher(str);
I have found a lot of good currency regular expressions that get very close to what I need. Alas, I am no regex guru and can't seem to edit my current regex to meet requirements.
I need to limit the valid inputs to the format of 'xxx,xxx.xx'. The max allowed amount needs to be '999,999.99' with commas optional. I've been using this regex until now:
^([0-9]{1,3}(,[0-9]{3})*|([0-9]+))(.[0-9]{2})?$
It has been working great except for not being able to make the upper limit '999,999.99'. Thanks for the help!
Update
I've been tinkering and I've managed to come up with this:
/^(?:([0-9]{3}?,?)?[0-9]{3}(?:\.[0-9]?[0-9]?)?)$/
Still testing to see if it works. RegexPlanet isn't passing it with any of the Strings I try, but I'll be going through my app and manually testing.
burning_LEGION's answer authorizes some cases I think you probably don't want:
- 999,9
- 9.
I'll assume you want those conditions fulfilled:
- if there is a comma, there are 3 numbers after
- if there is a point, there are 2 numbers after
^\d{1,3}(,?\d{3})?(\.\d{2})?$
use this regex ^\d{1,3}(,?\d{1,3}){0,1}(\.\d{0,2})?$
I have a little problem.
I have a text that i have to read in browser several time.
Everytime, I open this text, automatically start a replaceAll that i wrote.
It's very simple, basic but that problem is that when i do replace next time (every time i read this text) i have a replaceAll of replaceAll.
For example i have in the text:
XIII
I want to replace it whith
<b>XIII</b>
with:
txt.replaceAll("XIII","<b>XIII</b>")
The first time it's everything fine, but then, when i read again the text, it become:
<b><b>XIII</b></b>
It's a stupid problem, but i start now with Java.
I read that is possibile use regex.Could someone post a little example?
Thanks, and excuse me for my poor english.
You need negative lookbehind to prevent a match on an already marked-up string:
txt.replaceAll("(?<!>)XIII","<b>XIII</b");
This expression looks a bit convoluted, but this is how it decomposes:
(?<! ... ) is the template for the negative lookbehind;
> is the specific character we want to make sure doesn't occur in front of your string.
I should also warn you that fixing up HTML with regex's usually turns into a diabolic cycle of upgrading the regex to handle yet another special case, only to see it fail on the next one. It ends up with a monster that nobody can read, let alone improve.
There's a really fast solution. Do the opposite Replace before doing your own.
Let me show:
txt.replaceAll("<b>XIII</b>","XIII").replaceAll("XIII","<b>XIII</b>")
So you first turn your <b> into normal and than turn it back with <b> and it will achieve the same result without adding the new level of <b>.
What about this:
txt = txt.replaceAll ("XIII", "<b>XIII</b>").
replceAll ("<b><b>", "<b>").replaceAll ("</b></b>", "</b>");
I think <b><b> and </b></b> do not have much sense in HTML, so it is fine to remove duplicates even in other places.
Hi someone offered me a great solution that included \p{S} to match any symbol in a regex. The problem is that I need to eliminate two symbols from the \p{S}. I don't want & or ' to be a match.
I thought maybe \p{S^&^'} would work but it doesn't. I have looked online but I am not really sure what to search for.
Please help.
\b\p{L}*[\p{S}\p{P}]((\p{L}[\p{P}\p{S}])|([\p{P}\p{S}]\p{L})|(\p{L}))+\b
The other solution is \b([a-zA-Z]+(?:[^\w\s^'&]|_)[a-zA-Z]*)|[a-zA-Z]*(?:[^\w\s^'&]|_)[a-zA-Z]+\b but it catches words ending in punctuation. If it didn't do that it would work also.
Use character class subtraction (if available):
[\p{S}-[&']]
If not available, use a lookahead:
(?!.?[&'])\p{S}
You could use a negative look-ahead (?!.*[&']), i.e.
\b(?!.*[&'])\p{L}*[\p{S}\p{P}]((\p{L}[\p{P}\p{S}])|([\p{P}\p{S}]\p{L})|(\p{L}))+\b