Regex java operators - java

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).

Related

Simple Java regular expression matching fails

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);

Java regular expression for a digit followed by z^3?

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");

How to add a negation to \p{S}

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

understanding regex if then statements

So I'm not sure if I understand how this works and would like
a simple explanation to how they work is all. I probably have it way off. A pure regex solution is required, and I don't know if this is possible. If it is, a solution would be awesome too, but a shove in the right direction would be good for my learning process ^_^
This is how I thought the if/then/else option built into my regex engines was formatted:
?(condition)if regex|else regex
I want it to capture a string from a very specific location only when this string exists within a certain section of javascript. Because this is how I thought it worked after a decent amount of research I tried out a few variations of this code but they all ended up something like this.
((?^view_large$)Tables-137(.*?)search.htm)
Also of relevance: I'm using an java based app that has regex searches which pull the data I need so I cannot write an if statement in java which would be my preferred method. It's a pain to have to do it this way, but at the moment I have no other choice. I'm trying really hard for them to allow java code functionality instead of pure regex for more versatile options.
So to summarize, is there even a if/then option in regex and if so how is it formatted for what I'm trying to accomplish?
EDIT: The string that I want to be the "if condition" is like this: if view_large string exists and is not null then capture the exact string 500/ which is captured within the catch all group I used: (.*?)
There is no conditionals in Java regexp, but you can simulate them by writing two expressions that include mutually exclusive look-behind constructs, like this:
((?<=if )then)|((?<!if )end)
This expression will match "then" when it is preceded by an "if "; it will match "end" when it is not preceded by an "if "
The Javadoc for java.util.regex.Pattern mentions, in its list of "Perl constructs not supported by this class":
The conditional constructs (?(condition)X) and (?(condition)X|Y).
So, no dice. But you should look through the Javadoc to see if you can achieve what you need by using regex features that it does support. (Or, if you post some more detailed examples, we can try to help.)
Try lookaround assertions.
For example, say you want to capture FOOBAR only if there is a 4+ digit number somewhere:
(?=.*\d{4}).*(FOOBAR)

distinguishing a string with flex

I need to tokenize some strings which will be splitted of according to operators like = and !=. I was successful using regex until the string has != operator. In my case, string was seperated into two parts, which is expected but ! mark is in the left side even it is part of given operator. Therefore, I believe that regex is not suitable for it and I want to benefit from lex. Since I do not have enough knowledge and experience with lex, I am not sure whether it fits my work or not. Basically, I am trying to do replace the right hand side of the operators with actual values from other data. Do you people think that can it be helpful for my case?
Thanks.
Should you use lex? It depends how complex your language is. It's a very powerful tool, worth understanding (especially with yacc, or in Java you could use antlr or javacc).
public String[] split(String regex) does take a regex, not just a string. You could use the regex "!?=", which means zero or one ! followed by =. But the problem with using split is that it won't tell you what the actual delimiter was.
With what little info we have about your application, I'd be tempted to use regular expressions. There are lots of experts here on stackoverflow to help. A great place to start is the Java regex tutorial.
(Thanks to Falle1234 for picking up my mistake - now corrected.)

Categories