Regex to remove prefix and suffix in a string Java [closed] - java

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

Related

What's a good library for parsing mathematical expressions in java using user defined function which supports string as function parameter? [closed]

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.

How to use regular expressions to change a Java array in Atom? [closed]

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 4 years ago.
Improve this question
I need to move the 4th dimension of my Java array to the 1st dimension:
myArray[a][...][...][b]
to
myArray[b][...][...][a]
I need to do this for all dimension names. My idea is to do this using the find and replace feature in Atom using regular expressions, but I am not familiar enough with them to do it quickly. Basically, this is what I am looking for:
myArray([][][])([])
to
myArray([])([][][])
It would be wonderful if you could provide me with the required Find and Replace fields in order to do this.
Use this regex:
myArray(\[.+?])(\[.+?]\[.+?])(\[.+?])
And this replacement:
myArray$3$2$1
Explanation:
The regex is matching myArray followed by three sets of square brackets. It captures the first pair of brackets into group 1, second and third pairs into group 2, and the last pair into group 3.
The replacement is myArray, followed by whatever is in group 3, then whatever is in group 2, and finally whatever is in group 1.
Demo
This also works:
Find: myArray(\[[^\]]*\])(\[[^\]]*\]\[[^\]]*\])(\[[^\]]*\])
Replace: myArray$3$2$1

Compare substrig with split in java [closed]

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 6 years ago.
Improve this question
I have 'R-121|123|123' this type of string.
Now I want to split the above string in two sub string.
For example : Two sub string after splitting R and 121|123|123
I can split easily by using '-' this operator.
We can also do the same thing by using substring operator.
Which one is better (split or substring) ?
Split goes through your entire string, and creates new objects plus the array object itself,
So substring would definitely be more efficient
this may be useful :
http://www.gettingcirrius.com/2010/11/performance-comparison-string-split-vs.html
Split is more clean way. If data set is small split can be used.
Regarding performance i am not very clear how substring will be better than split.
I have also read that substring is better but i am not convinced because in case of substring also string traversal is required to find index. so traversing time should be same. But yes split will create all new objects based on delimiter, which will be garbage collected. So as per my understanding, if requirement is to get all the tokens then use split but if specific occurrence is needed then better to go for substring.

How do I extract the following patterns in java [closed]

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.

How to validate math formular string using regex? [closed]

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.

Categories