Jump to content

Regular expressions


Recommended Posts

Couple of questions about regular expressions.

1. Supposing I have a string such as "The cat sat on the mat". Is it possible to construct a regular expression that would remove all a's between "cat" and "mat", while leaving "cat" and "mat" unchanged? (Output would be "The cat st on the mat".)

2. I want to search for strings such as "_some text_" and replace the underscores with "<i>" and "</i>", while leaving the text between them unchanged. (Output would be "<i>some text</i>".) Is this possible?

Thanks!

Link to comment
Share on other sites

I'm not very good at Regular expressions but this will do just fine on the second one :)

$string="_some text_ _some more text_"

Do
$string=StringRegExpReplace(StringRegExpReplace($string,"_*_","<i>",1),"_*_","</i>",1)
Until @extended=0

MsGbox(0,"Formatted string",$string)

:P

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

  • Moderators

Pretty fugly:

$s_1 = "The cat sat on the mat"
$s_out_1 = StringRegExpReplace($s_1, "(cat)((.*?)(a)(.*?))*(mat)", "\1\3\5\6")
ConsoleWrite($s_out_1 & @CRLF)

$s_2 = "_some text_ and we can go _on more to see if it_ works or not"
$s_out_2 = StringRegExpReplace($s_2, "(_)(.*?)(_)", "<i>\2\<\\i>")
ConsoleWrite($s_out_2 & @CRLF)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Many thanks to everyone for the replies! Still trying to get my head round them, as they are way beyond my current level, but hopefully they should solve the problem!

Just a quick question , SmOke_N: could you explain what the characters "\1\3\5\6" mean in the first expression? Sorry for my ignorance, but I haven't come across anything like that before.

Link to comment
Share on other sites

  • Moderators

Many thanks to everyone for the replies! Still trying to get my head round them, as they are way beyond my current level, but hopefully they should solve the problem!

Just a quick question , SmOke_N: could you explain what the characters "\1\3\5\6" mean in the first expression? Sorry for my ignorance, but I haven't come across anything like that before.

Well the first expression without a loop doesn't really matter anyway, as I can't get it to backtrack through all the "a"'s.

The \1\3 etc... those are back referencing the data between the parenthesis to display again (A quick look in the help file under that function would have shown you that :) ).

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Well the first expression without a loop doesn't really matter anyway, as I can't get it to backtrack through all the "a"'s.

The \1\3 etc... those are back referencing the data between the parenthesis to display again (A quick look in the help file under that function would have shown you that :) ).

Well, I did look at the help file, as well as the one for String RegExp, but I didn't really understand it. Guess I'll have to find a beginner's guide to regular expressions! Must be one on the net somewhere...

Link to comment
Share on other sites

  • Moderators

Well, I did look at the help file, as well as the one for String RegExp, but I didn't really understand it. Guess I'll have to find a beginner's guide to regular expressions! Must be one on the net somewhere...

I was referring to the back reference answer being in the help file in the parameters information.

http://weitz.de/regex-coach/

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...