Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Regex help

  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Unhappy Regex help

    Hi

    I've never worked with regex before, so this is somewhat guess work.

    If I have a line of

    YUN: A-ZP002920176 (Priority 4) - Shipped

    in an email subject.

    How can I extract

    A-ZP002920176

    with a regex?

    I had

    var rxIncidentNum = /(^A-IM\d*)\*/i;

    to be used within other script, and it's not working. Is my regex the problem?

    Any help or suggestions appreciated!!!
    thanks.
    - n.

  • #2
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts

    further to that!

    .....more regex. If I have content in an email like

    Urgency: 7
    Incident Title: Fault: IP phone at Store
    Incident Description: Asset Number: 591Lblips1


    Can I write regex that will extract everything after Title: and stop at the end of the line?

    Regards
    Narelle

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <script type = "text/javascript">
    
    var str = "YUN: A-ZP002920176 (Priority 4) - Shipped"
    str = str.replace (/^(.*\:\s)/,"").replace (/\s.*/,"")
    alert (str);  //A-ZP002920176
    
    </script>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    narelle (07-27-2013)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by narelle View Post
    .....more regex. If I have content in an email like

    Urgency: 7
    Incident Title: Fault: IP phone at Store
    Incident Description: Asset Number: 591Lblips1


    Can I write regex that will extract everything after Title: and stop at the end of the line?

    Regards
    Narelle
    Is the email plain text or HTML? Try this:-


    Code:
    <script type = "text/javascript">
    
    var txt = "Urgency: 7 <br>Incident Title: Fault: IP phone at Store<br>Incident Description: Asset Number: 591Lblips1"
    txt = txt.replace (/<br>/gi, "~");  // HTML format
    txt = txt.replace(/(\\r)/g, "");  // Plain text
    txt = txt.replace (/(\\n)/g, "~");
    txt = txt.replace(/^.*Title:\s/,"").replace(/~.*/i,"");
    
    alert (txt); // Fault: IP phone at Store
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi,

    Thank you for replying, so I tried to work with the first regex you gave me without success, but perhaps my issue lies elsewhere as it failed to do what I was expecting:


    var strSubjectLine = new String(email.body_text);
    var rxMatch;
    var rxIncidentNum = strSubjectLine.replace (/^(.*\:\s)/,"").replace (/\s.*/,"");
    var strIncidentNum;
    if (strSubjectLine.search(rxIncidentNum) >= 0) {
    rxMatch = strSubjectLine.match(rxIncidentNum);
    strIncidentNum = rxMatch[1];
    if (typeof(strIncidentNum) != 'undefined') {
    externalReferenceNumberCreate(current.sys_id, 'Client reference number', strIncidentNum, 'Ref number');
    }
    }

    But in your example, you've said to look between the quotation marks right? I need to tell it to look at the email subject. Sorry for the newbie questions!

    thanks
    Narelle

  • #6
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Oops, that first line should be
    var strSubjectLine = new String(email.subject);

    I think.

  • #7
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    836
    Thanks
    172
    Thanked 90 Times in 90 Posts
    hi

    try this

    I used this as first line to test
    var strSubjectLine = "YUN: A-ZP002920176 (Priority 4) - Shipped";
    worked ok up to typeof, after that I can't check.

    Code:
    var strSubjectLine = new String(email.subject);
    var rxIncidentNum = strSubjectLine.replace (/^(.*\:\s)/,"").replace (/\s.*/,"");
    //alert("rxIncident "+rxIncidentNum); //ok
    
    var rxMatch;
    var strIncidentNum;
    if (strSubjectLine.search(rxIncidentNum) > 0) {
    rxMatch = strSubjectLine.match(rxIncidentNum);
    strIncidentNum = rxMatch[0];
    //alert(strIncidentNum); //ok
    //alert(typeof(strIncidentNum)); //string
    if (typeof(strIncidentNum) != 'undefined') {
    externalReferenceNumberCreate(current.sys_id, 'Client reference number', strIncidentNum, 'Ref number');
    	}
    }
    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    narelle (07-27-2013)

  • #8
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Is the email plain text or HTML? Try this:-


    Code:
    <script type = "text/javascript">
    
    var txt = "Urgency: 7 <br>Incident Title: Fault: IP phone at Store<br>Incident Description: Asset Number: 591Lblips1"
    txt = txt.replace (/<br>/gi, "~");  // HTML format
    txt = txt.replace(/(\\r)/g, "");  // Plain text
    txt = txt.replace (/(\\n)/g, "~");
    txt = txt.replace(/^.*Title:\s/,"").replace(/~.*/i,"");
    
    alert (txt); // Fault: IP phone at Store
    
    </script>

    This didn't work for me, it just returned the email body (before the field ran out of characters.

    The email has lots of information and the incident description line is buried in the middle, so maybe regex is not the best way. I usually work with javascript but struggled there as well! I should try search and substring I guess. Regex looks powerful to me but I might never get my head around it.

    Regards
    Narelle

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    It works for me, but you do not make it entirely clear what you are trying to do.

    Change this line to

    txt = txt.replace(/.*Title:\s/,"").replace(/~.*/i,"");

    (delete the caret).

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    narelle (07-27-2013)

  • #10
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    ThanksPhilip,

    Yes, it works for me when I test it in a test script window like this -

    var txt = "Incident Title: Fault: IP phone at Clinic";
    txt = txt.replace(/(\\r)/g, "");
    txt = txt.replace (/(\\n)/g, "~");
    txt = txt.replace(/.*Title:\s/,"").replace(/~.*/i,"");
    gs.print(txt);

    but once I specify that
    var txt = email.body_text;

    it just puts the whole email body in. It's a text email btw. Am I missing something completely obvious here?

    I'm really new to this, sorry.

    I also tried

    var str = email.body_text;
    var n = str.lastIndexOf('Title:');
    var result = str.substring(n + 6);
    result;

    which started where I wanted it to but then ran into the next line, like this:
    Fault: IP phone at ClinicIncident Description: Asset Number:

    as Incident Description is on the next line in the email.

    Regards
    Narelle

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    
    var str = email.body_text; 
    var n = str.lastIndexOf("Title:");
    var q = str.lastIndexOf("Incident Description");
    var result = str.substring(n + 6, q);
    alert (result);
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    narelle (07-28-2013)

  • #12
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you!! Appreciate your patience with me, that worked perfectly & handy for future uses as well.

    Regards
    Narelle

  • #13
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by narelle View Post
    .....more regex. If I have content in an email like

    Urgency: 7
    Incident Title: Fault: IP phone at Store
    Incident Description: Asset Number: 591Lblips1


    Can I write regex that will extract everything after Title: and stop at the end of the line?
    You can do this by using a backreference:

    Code:
    <!doctype html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8">
    		<title>Demo</title>
    		<script>
    			var text = "Urgency: 7\r\nIncident Title: Fault: IP phone at Store\r\nIncident Description: Asset Number: 591Lblips1";
    			text = text.match(/Incident Title: ([^\r\n]+)/)[1];
    			document.defaultView.alert(text);
    		</script>
    	</head>
    	<body></body>
    </html>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    narelle (07-28-2013)

  • #14
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by narelle View Post
    Thank you!! Appreciate your patience with me, that worked perfectly & handy for future uses as well.
    OK, looks like I was too slow to post lol
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #15
    New Coder
    Join Date
    May 2013
    Posts
    24
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks - that works too, certainly helpful to have on hand. It worked neatly for other information I needed to extract. Now I just need to consult the regex reference and work backwards to see what you've done

    This forum is great.

    Thanks again


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •