Regexp replace that acts like a case statement

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
hfeldma1
Forum Newbie
Posts: 2
Joined: Tue Nov 03, 2009 11:11 am

Regexp replace that acts like a case statement

Post by hfeldma1 »

I'm trying to do a regexp replace.

I have the following regexp
^(?s)(?:.*)(?:Resistance Z/F A-Pair TG\s*)(\w+)(?:.*)

on the following source example.
Resistance Z/F A-Pair TG OR Kohms
Resistance Z/F A-Pair TG 1234 Kohms

It currently returns the "OR" for example #1 and 1243 for example #2, exactly what I wanted, however I need to change/replace the OR to a zero (0) for example #1 but still return 1234 for example #2.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Regexp replace that acts like a case statement

Post by ridgerunner »

You need to explain more precisely exactly what you are trying to achieve. Are each of the two lines in your text in separate strings or in one string? Is the (0) the ASCII number zero '0'=\x30 or the actual null character = \x00?

If all you are trying to achieve is to match every 'OR' which is imnmediately preceeded by the text 'Resistance Z/F A-Pair TG\s*', and replace that 'OR' with a '0' then you could use this:

Code: Select all

$result = preg_replace('%(Resistance Z/F A-Pair TG\s*)OR%', '${1}0', $subject);
Please describe exactly what you wish to do and provide plenty of before and after examples of text that both match and don't match.
hfeldma1
Forum Newbie
Posts: 2
Joined: Tue Nov 03, 2009 11:11 am

Re: Regexp replace that acts like a case statement

Post by hfeldma1 »

The examples I provided are from two different files. I’m going to be parsing 1000’s of files containing either example #1 or #2.
i.e. Example #1
Resistance Z/F A-Pair TG OR Kohms
Example #2
Resistance Z/F A-Pair TG 1234 Kohms

Currently this is the rexexp I use to parse both example #1 and #2.
^(?s)(?:.*)(?:Resistance Z/F A-Pair TG\s*)(\w+)(?:.*)

For Example #1 “OR” is return and for Example #2 “1234” is returned.

What I would like returned is
Example #1
0 (ascii x30)
Example #2
1234 (ascii x31x32x33x34)

The language I'm using is java, and I have no problem in doing this in two steps. i.e.
Pattern compiledRegex = Pattern.compile(Regexp);
// Create the object that will search the subject string
// using the regular expression.
Matcher regexMatcher = compiledRegex.matcher(InterfaceName);
boolean matchFound = regexMatcher.find();
if (matchFound) {
// Get the first group for this match
groupStr = regexMatcher.group(1);
I could do a second regexp function here on groupStr
} else {
groupStr = "Error Parsing";
}
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Regexp replace that acts like a case statement

Post by ridgerunner »

I'm sorry but I still don't understand what you are trying to accomplish. Your regex matches the whole target string (entire contents of the file) and captures the first 'OR' or '1234' (or anything else matching (\w+) in this position) into capture group 1. Are you planning on doing anything with the data captured in group 1? If so, what exactly? Are you changing the contents of files? Do the files have more than one line of text?

We still need more clarification and examples. Post the full contents of several example files as well as what they should look like once they are processed. And post the full text of the java code you have so far.
Post Reply