Solved

Java replaceAll regex

Posted on 2009-04-02
7
1,580 Views
Last Modified: 2013-11-23

 Hi Experts,
 
    I want to replace the following string embeded in an input line
   AFunctionName[startPosition , endPosition]  
   with
   SUBSTR(AFunctionName, startPosition , endPosition)
 
   match requirements:
   1.  space in AFunctionName[startPosition , endPosition],  before and after comma ","  is option;
   2. All names, AFunctionName, startPosition, endPosition are made of  word characters

   I wrote a method to parse it doesn't work, any help is appreciated.

0
Comment
Question by:SarahDeng
  • 4
  • 3
7 Comments
 
LVL 40

Accepted Solution

by:
RQuadling earned 300 total points
ID: 24055081
Try this ...
StringBuffer resultString = new StringBuffer();

try {

	Pattern regex = Pattern.compile("(\\w+)\\[(\\w+) , (\\w+)\\]", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);

	Matcher regexMatcher = regex.matcher(subjectString);

	while (regexMatcher.find()) {

		try {

			// You can vary the replacement text for each match on-the-fly

			regexMatcher.appendReplacement(resultString, "SUBSTR($1, $2, $3)");

		} catch (IllegalStateException ex) {

			// appendReplacement() called without a prior successful call to find()

		} catch (IllegalArgumentException ex) {

			// Syntax error in the replacement text (unescaped $ signs?)

		} catch (IndexOutOfBoundsException ex) {

			// Non-existent backreference used the replacement text

		} 

	}

	regexMatcher.appendTail(resultString);

} catch (PatternSyntaxException ex) {

	// Syntax error in the regular expression

}

Open in new window

0
 

Author Closing Comment

by:SarahDeng
ID: 31566024
Can't expect better and quicker than this!
0
 

Author Comment

by:SarahDeng
ID: 24055341
Hi RQuadling,
Oops! there is one exception.
It doesn't work when there is no space before and after the ","
example:
 No space in AFunctionName[startPosition,endPosition],  before and after comma ","
I change the expression to the following, it doesn't work for me.  Could you help again? Thanks!
 "(\\w+)\\[(\\w+)(\\s*),(\\s*) (\\w+)\\]",
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:RQuadling
ID: 24055392
Try this ...
StringBuffer resultString = new StringBuffer();

try {

	Pattern regex = Pattern.compile("(\\w+)\\[(\\w+)\\s+?,\\s+?(\\w+)\\]", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);

	Matcher regexMatcher = regex.matcher(subjectString);

	while (regexMatcher.find()) {

		try {

			// You can vary the replacement text for each match on-the-fly

			regexMatcher.appendReplacement(resultString, "SUBSTR($1, $2, $3)");

		} catch (IllegalStateException ex) {

			// appendReplacement() called without a prior successful call to find()

		} catch (IllegalArgumentException ex) {

			// Syntax error in the replacement text (unescaped $ signs?)

		} catch (IndexOutOfBoundsException ex) {

			// Non-existent backreference used the replacement text

		} 

	}

	regexMatcher.appendTail(resultString);

} catch (PatternSyntaxException ex) {

	// Syntax error in the regular expression

}

Open in new window

0
 

Author Comment

by:SarahDeng
ID: 24055491
I tried... It doesn't work with the case that no space before and after the ","
Like this.
string_variable[startPosition,endPostion]

0
 

Author Comment

by:SarahDeng
ID: 24055529
Changed the expression to
"(\\w+)\\[(\\w+)\\s*,\\s*(\\w+)\\]"
it works.
Can you explain why?
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 24057754
Ah Yes.

\s+\s*

Options: case insensitive; ^ and $ match at line breaks

Match a single character that is a whitespace character (spaces, tabs, line breaks, etc.) «\s+?»
   Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»

Match a single character that is a whitespace character (spaces, tabs, line breaks, etc.) «\s*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»


I was assuming at least 1 space. Sorry.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 79
Spring Framework HTTPSession management 1 23
Grep - regex - find files matching pattern 2 44
object oriented programming comparison 5 55
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now