Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a textbox to input a mathematical formular inlcude +,-,*,/,(,),TRUNC,ROUND,POWER,MOD,SQRT,FLOOR,DECODE. After user input a formula string, i want to validate this string but i don't know how ???
Please help me out.
Unfortunately you can't validate such expressions using regex. The nature of the regular expressions is so, that you never can validate matching parenthesis. Regex is simply to weak.
For more information why it is so: http://en.wikipedia.org/wiki/Regular_expression
In order to validate/parse or evaluate mathematical expressions you need a context free grammar parser. You can relatively simple generate one using one of parser generators. I would recommend
JavaCC: https://javacc.java.net/
Antlr: http://www.antlr.org/
Context free grammars: http://en.wikipedia.org/wiki/Context-free_language
Have a look at this question on Code Review. It shows some code for parsing such expressions and the answers give examples on how to alternatively use the scripting engine that ships with Java.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Not suitable for this site
Improve this question
What's a good library for parsing mathematical expressions in java with user defined function which supports string as function parameter?
Any new updated library or we need to re-write this kind of library.
Eg.
"10+ userDefineFunction('parameterName')*70".
You can use this regex:
([A-z][A-z]+)\('([^']+)'
group 1 matches the method name, group 2 matches the string parameter, test it here.
Of course this has some potential for improvements:
method name is letters only
no (escaped) ' can appear inside the parameter string
but it works for the current example.
EDIT:
in java, using the regex looks like this:
String mathExp = "10+ sumRun('playerName')*70;";
Matcher matcher = Pattern.compile("([A-z][A-z]+)\\('([^']+)'").matcher(mathExp);
matcher.find();
String methodName = matcher.group(1);
String parameter = matcher.group(2);
The best way to start is to determine the syntax for identifying what a method call would look like. Once the syntax has been identified, you'll need to create a lexical analyzer. Do searches on the key terms here. They will give you a good start into other threads for other approaches. Another good key term would be regular expression or regex.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to remove prefix and suffix in a String and extract the middle portion of the string.
For eg: Consider the Strings - "www.hello.com" and "www.test.com"
Here prefix - "www." and suffix - ".com". I want to extract the middle words - hello and test.
Currently i have achieved this using String.replace() method in Java.
str.replace("www.","").replace(".com","");
I want know is there any regular expression available to achieve it in a single method in java.
You could use a regex for that, it would work in the same way. Your regex would simply contain a capture group with both the prefix and the suffix in an OR operation.
(www\.|\.com)
You could then use this like you did with the replace.
String test = "www.test.com";
String output = test.replaceAll("(www\\.|\\.com)","")
P.S. this code is untested. Please don't just copy and paste it expecting everything to work.
(?<=www.)(.*)(?=.com)
This uses the lookbehind and lookahead feature of regex
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have CSV file which contains following line.
INPUT:
No,NAme,ID,Description
1,Stack,232,"ABCDEFGHIJKLMNO
-- Jiuaslkm asdasdasd"
2,Queue,454,"PQRSTUVWXYZ
-- Other
words here"
3,Que,4343,"sdfwerrew"
OUTPUT EXPECTED:
No,NAme,ID,Description
1,Stack,232,"ABCDEFGHIJKLMNO \n -- Jiuaslkm asdasdasd"
2,Queue,454,"PQRSTUVWXYZ \n -- Other \n words here"
3,Que,4343,"sdfwerrew"
or
No,NAme,ID,Description
1,Stack,232,"ABCDEFGHIJKLMNO -- Jiuaslkm asdasdasd"
2,Queue,454,"PQRSTUVWXYZ -- Other words here"
3,Que,4343,"sdfwerrew"
Is there any java regex pattern available to find and merge the lines based starting double quotes and end quotes?
You are going down the wrong path. Not everything should be solved using regular expressions. CSV parsing is one of those things.
Seriously: you are about to re-invent the wheel. And the wheel you are about to create will be deficient, and prone to break over and over again.
The sane approach: there are many existing CSV parsers for Java out there. They deal perfectly with multi-line values. So: use one of them (see here as starting point for the many choices you have)
There is a nice rule of thumb: when your regex becomes so complicated that you can't write it down yourself; then consider doing things differently. You are the person who owns this code; you will have to maintain and maybe enhance it - not those folks here that are able to write down a regex that solves this one flavor of CSV example input.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have string in following format:
String s = " some text....
[[Category:Anarchism| ]]
[[Category:Political culture]]
[[Category:Political ideologies]]
[[Category:Far-left politics]]
... some more text"
I want to extract all the categories from this text. [Anarchism,Political culture ....,Far-left politics]
Also, is there a good tutorial where I can learn about this regex pattern matching stuff..
Thanks
You can use the following regex to get categories:
\[\[Category:(.+)\]\]
Then you can access to your groups to get the category values.
Remember to add backslash to backslashes if you use on java strings:
\\[\\[Category:(.+)\\]\\]
You can see it working:
Assuming you don't want to select the word "Category" itself, the regex would be:
(?<=Category:).*?(?=])
I'll break this down a bit for you.
The first bit in brackets looks for Category, without actually selecting it.
Next .+? looks for 1-infinity characters (other than a newline), but stops as soon as the next part is matched:
The final brackets tells it to look for a ], but without actually selecting it.
The results would be the bits below highlighted in blue.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am writing a simplified Java compiler. I wrote a regex for variable name:
"(_?[a-zA-Z]+[\w]*)"
and I want to add that the name can not be certain words, like int, double, true, false...
I tryed using ^ , but it is not working.
It can be done with a RE, but it's not easy for a human to write it. Treat keywords as identifiers in the scanner and distinguish the identifiers vs keywords in the tokenizer afterwards. That should be substantially easier.
I don't believe that this should do that via regular expressions but rather can be better done using a HashSet<String> and exclude identifier names that are contained in the set.
^ is used for something else :
^ may appear at the beginning of a pattern to require the match to
occur at the very beginning of a line. For example, ^abc matches
abc123 but not 123abc.
consider using "(?!...)" :
(?!...) is a negative look-ahead because it requires that the
specified pattern not exist.
i suggest that if it's impossible or too hard , go to real coding instead . sometimes , regular expressions can be much slower than real , optimized code , and they can be very confusing and you might have problems finding what's wrong with what you've written.
for trying out your regular expressions , check this one:
http://gskinner.com/RegExr/
for quick referencing , check this one:
http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm