简体   繁体   中英

How to get resource name from resource assignment

I'm using MPXJ to read mpp file. I have a resource assignment string as below:

string st = [[Resource Assignment task=Sign contract and update data resource=
X.C. Information Management start=Thu Jun 09 08:00:00 ICT 2014 finish=Thu Jun 05 17:
00:00 ICT 2014 duration=32.0h workContour=null]]

I want to get resource name from the above string ( ). )。
Currently, I use the code:

st.Split('=')[2].Replace(" start", ""); // return X.C. Information Management<br/>

I think use regular expression as well as, howerver, I don't have any ideas to implement it.
Please help me if you can.

Thanks

If the information you want is wrapped in bold tags ( <b> & </b> ), and there are no other bold tags in your string, then this regex should work:

(?<=<b>).*(?=<\/b>)

See here

In C#, you could do something like this:

Regex regex = new Regex(@"(?<=<b>).*(?=<\/b>)");
string testString = @"*string st = [[Resource Assignment task=Sign contract and update data resource=<b>X.C. Information Management</b> start=Thu Jun 09 08:00:00 ICT 2014 finish=Thu Jun 05 17:00:00 ICT 2014 duration=32.0h workContour=null]]*";
string text = regex.Match(testString).Value;

And text will equal XC Information Management

EDIT: Ok - so the OP removed the <b> tags, but the principle is still very much the same. Just replace the <b> tags with an appropriate marker that you know will come before and after the string you are looking for. For example:

(?<=resource=).*(?=start)

You can use a Regular Expression like this:

resource=(.*)\sstart=

The information would be in the group not in the matched string:

Regex.Match(input, "resource=(.*)\sstart=").Groups[1].Value

Interesting... it looks like the OP has retrieved a Resource instance from MPXJ, and is using the toString() method of the instance to look at the data, for example:

Project file;
// Some code to read the project

// Retrieve the first resource
Resource resource = file.getResourceByUniqueID(1);
string st = resource.toString();

This is not a great idea... the toString() method is intended to provide debugging information only.

A better approach would be to call a method to retrieve the name directly:

string st = resource.getName();

You can find API documentation for MPXJ here which will give you an idea of the methods available on each object, if you IDE doesn't provide this for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM