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'm trying to split a string of the following form using a regex:
"/say hello world (hello there) (how (are you))"
This should split into:
/say
hello
world
(hello there)
(how (are you))
Splitting on " "(space) obviously doesn't work, as I don't want to split strings inside brackets. How can something like this be achieved?
Also, worth noting: I'm using PCRE for parsing.
You can create your own parser for this:
1. Iterate over string characters to find " ", if you find ( increase one counter (create in your method) and when you find ) decrease it.
2. If you find " " and your counter is 0 then take substring till that point and add in an array, (reset internal variables to if any to work in loop).
3. if you find " " and counter is still not zero then ignore and go to step 1 again.
Related
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 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
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 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 7 years ago.
Improve this question
I'm developing an application where the user will insert an vehicle ID.
At our country, the ID is always 3 letters and 4 numbers.
How can I check if a String has X numbers and Y letters in Java?
You can simply work with regular expressions. Something like:
if(vehicleId.matches("^[A-Z]{3}\d{4}$"))
(Assuming the id contains three capital letters followed by four digits.)
This will return a boolean if vehicleId, supposedly a variable holding the user's input, is matched.
There are many solutions for this problem. I'll not give you a full solution but will try to guide you.
One solution would be iterating on the string char-by-char. The Character class contains many useful methods for this task.
Other solution would be using a regex and replaceAll non digits characters (\D) with the empty string. From here the way to the answer is very short.
Visit the String API and the regex tutorial to fuel your creative fire.
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.