从字符串中提取字符串 [英] Extracting a string from a string

查看:132
本文介绍了从字符串中提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我查看的字符串可以是任意长度,可以包含任何字符,是否有一种简便的方法来检查主字符串中是否有以"["开头并以]"结尾的数据字符串?并提取该文本,并用方括号括起来?

例如:

这是我不感兴趣的数据-被忽略
此数据有一个方括号[因此我不感兴趣-这被忽略
此数据有一个右括号],所以我不感兴趣-这被忽略了
该数据包含[看着我],因此我需要-提取了[看着我]

希望很清楚,这不容易解释!!!

我意识到我可以使用一个循环来找到一个左括号,再使用一个循环来找到一个右括号,然后使用它们的位置提取数据,但这似乎很费劲...

If I look at a string that can be any length and can contain any characters, is there an easy way of checking if there is a string of data within the main string that starts with "[" and ends with "]" and extract that text, with the square brackets included ?

For example :

This is data that I am not interested in - This is ignored
This data has an opening bracket [ so I''m not interested - This is ignored
This data has a closing bracket ] so I''m not interested - This is ignored
This data has [Look at me] in it so I want it - [Look at me] is extracted

Hope that is clear, it wasn''t easy to explain !!!

I realise that I could use a loop to find an opening bracket & another to find a closing bracket & then using their positions, extract the data, but that seems long-winded ...

推荐答案

最简单的方法是使用正则表达式:

Easiest way is to use a Regex:

\[.*?\]

可以做到.

will do it.

Public Dim regex As Regex = New Regex("\[.*?\]")

...

Dim m As Match = regex.Match(InputText)
If m.Success Then
    Dim matched As String = m.Value
    ...
End If



行之有效,好极了……只要有机会您可以解释它在做什么,以及它为何起作用?!?!?"

哇,男孩!现在有一个* BIG *问题! :laugh:

正则表达式(或正则表达式)是基于文本的模式匹配器-它们在字符串中查找指定的模式,并可以对它们执行复杂的操作.
我什至不会尝试在这里查看全部详细信息-我需要整夜输入一半的信息-但实际表达非常简单:



"That works, brilliant ... any chance you can explain what it''s doing, how & why it works ?!?!?"

Whoo boy! Now there is a *BIG* question! :laugh:

Regular Expressions (or Regex for short) are text based pattern matchers - they look for specified patterns within strings and can perform complicated manipulations on them.
I won''t even try to go into the full details here - it would take me all night to type even half of them - but the actual expression is pretty simple:

\[.*?\]

是匹配器正在寻找的模式,就像正则表达式一样,它非常非常简单.
首先,匹配项是一个普通的字符串,但是几乎每个字符都具有某种含义.
首先要注意的是"\"是一个特殊字符,它表示下一个字符不是特殊字符,它是一个文字,因此请别管它".这意味着"\ ["是单个文字"["字符(而"\\""是单个]")-我们需要在"'[,因为方括号在正则表达式中具有特殊含义.
接下来,我们有一个.''-正则表达式,代表完全没有字符".
后跟一个"*"-表示我剩下的任何数字(包括零)"
然后是一个?",通过在末尾添加"...但应尽可能少"来修改"*"的含义.

因此,它正在寻找的模式是:

Is the pattern the matcher is looking for, and it''s very, very simple as Regexes go.
First off, the match is a normal string, but nearly every character means something.
The first thing to note is the ''\'' is a special character which says "the next character is not a special character, it is a literal so leave it alone". Which means that ''\['' is a single literal ''['' character (and ''\]'' is a single '']'') - we need to include the ''\'' before the ''['' because square brackets have a special meaning in Regexes.
Next we have a ''.'' - which is Regex for "any character at all".
Followed by a ''*'' - which means "any number (including zero) of whatever is immediately left of me"
Then a ''?'' which modifies the meaning of the ''*'' by adding on the end "...but as few as possible"

So the pattern it is looking for is:

\[ . * ? \]
An open square bracket followed by any number of any character ending with the first instance of a close square bracket.



您正在寻找从字符串中提取的内容.

正则表达式可能变得更加复杂:您可以命名它的一部分,然后执行此列表中的任何字符"(如果您不将反斜杠放在它们前面,则用方括号来表示),或者指定3到9之间的字符数,以及其他种类繁多的内容.

他们值得学习;对于他们适合的任务,它们功能强大,可以节省大量的慢速代码.不利的一面是,它们难以维护和阅读,特别是在您入门时! :laugh:

如果您想和他们一起玩(这是个好主意),请获取 Expresso [ ^ ]-它是免费的,它检查并生成正则表达式.我经常使用它进行测试,但愿我会写它!

有整本书专门介绍您可以使用正则表达式做什么,但是这里有一个教程 The 30分钟正则表达式教程 [ ^ ]将会解释很多有关它们的信息.



Which is what you were looking to extract from your string.

Regexes can get a lot more complex: You can name parts of it, and do "any of the characters in this list" (which is what the square brackets do if you don''t put the back slash in front of them) or specify a number of character between 3 and 9, and a huge range of other things.

They are well worth learning about; for the tasks they are suited to they are incredibly powerful and can save a huge amount of slow code. The down side of this is that they are a pain to maintain and read, particularly when you are getting started! :laugh:

If you want to play with them (and it''s a good idea) get a copy of Expresso [^] - it''s free, and it examines and generates Regular expressions. I use it a lot for testing, and I wish I''d written it!

There are whole books devoted to what you can do with regexes, but there is a tutorial here The 30 Minute Regex Tutorial[^] which will explain a good amount about them.


这篇关于从字符串中提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆