473,385 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Java Regex: Parsing Problem

44
Hi everyone!

How to split a string with parenthesis, since the parenthesis are characters used in regular expressions for grouping?

For example, how to split the string "by Gregor Hohpe and Bobby Woolf (2003)" in a way that the result gives 2 strings: "by Gregor Hohpe and Bobby Woolf " and "(2003)"?

I´ve tried unsuccessfully with several regular expressions like "(([0-9]))", and i´m really out of ideas. I would be much appreciated for any ideas for a regular expression.

Best Regards
Aug 6 '07 #1
4 3661
JosAH
11,448 Expert 8TB
Hi everyone!

How to split a string with parenthesis, since the parenthesis are characters used in regular expressions for grouping?

For example, how to split the string "by Gregor Hohpe and Bobby Woolf (2003)" in a way that the result gives 2 strings: "by Gregor Hohpe and Bobby Woolf " and "(2003)"?

I´ve tried unsuccessfully with several regular expressions like "(([0-9]))", and i´m really out of ideas. I would be much appreciated for any ideas for a regular expression.

Best Regards
You can escape these special characters: \( and \). Note that the backslash is
a special character for the javac compiler as well, so it you want to pass these
characters to the regexp compiler in a String literal you have to use \\( and \\)

Note that you can't 'count' matching parentheses using regular expressions, i.e.
if you want to check whether or not parentheses are balanced you can't use
regular expressions for that.

kind regards,

Jos
Aug 6 '07 #2
prometheuzz
197 Expert 100+
You can escape these special characters: \( and \). Note that the backslash is
a special character for the javac compiler as well, so it you want to pass these
characters to the regexp compiler in a String literal you have to use \\( and \\)
@OP:
And if the you want to keep the parenthesis in tact, you will have to use some sort of look around-voodoo:
Expand|Select|Wrap|Line Numbers
  1. String[] array = s.split("(?=\\()|(?<=\\))");

Note that you can't 'count' matching parentheses using regular expressions, i.e.
if you want to check whether or not parentheses are balanced you can't use
regular expressions for that.

kind regards,

Jos
I believe some regex engines can do such recursive things (Perl's if I'm not mistaken?). But you're right, Java's regex certainly can't.
Aug 6 '07 #3
JosAH
11,448 Expert 8TB
I believe some regex engines can do such recursive things (Perl's if I'm not mistaken?). But you're right, Java's regex certainly can't.
Perl is a write only atrocity ;-)

kind regards,

Jos (I didn't write any of the above, honest! ;-)
Aug 6 '07 #4
zeny
44
Thanks to you all, now i´ve made it! Very much apreciated for the help!

Best regards
Aug 7 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: John Smith | last post by:
Hello, I have a rather odd question. My company is an all java/oracle shop. We do everything is Java... no matter what it is... parsing of text files, messaging, gui you name it. My question...
3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
13
by: Chris Lieb | last post by:
I am trying to write a regex that will parse BBcode into HTML using JavaScript. Everything was going smoothly using the string class replace() operator with regex's until I got to the list tag....
24
by: cassetti | last post by:
Here's the issue: I have roughly 20 MS excel spreadsheets, each row contains a record. These records were hand entered by people in call centers. The problem is, there can and are duplicate...
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
0
by: vmysore | last post by:
I am trying to get all the columns selected within a SQL query (including the sub selects). When the code hits matcher.find(). i get the following exception: Exception in thread "main"...
3
by: sukatoa | last post by:
Good day Is it safe to use java regex to extract links and email addresses of a specific webpage?(this is not intended for malicious activity) We are on the final stage in college, and we have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.