English 中文(简体)
Regex - 将所有不包含“/”字
原标题:Regex - Get all words that are not wrapped with a "/"

Im really trying to learn regex so here it goes.

I would really like to get all words in a string which do not have a "/" on either side. For example, I need to do this to: "Hello Great /World/" I need to have the results: "Hello" "Great"

如果是的话,那么我怎么做呢? 我认为,希望把结果储存在一阵列中:

谢谢。

最佳回答

Just use this regular expression (?<!/)w+(?!):

var str = "Hello Great /World/ /I/ am great too";
var words = Regex.Matches(str, @"(?<!/)w+(?!/)")
    .Cast<Match>()
    .Select(m=>m.Value)
    .ToArray();

这将使你:

Hello
Great
am
great
too
问题回答
 var newstr = Regex.Replace("Hello Great /World/", @"/(w+?)/", "");

如果你真的想要有一系列的扼杀。

var words = Regex.Matches(newstr, @"w+")
    .Cast<Match>()
    .Select(m => m.Value)
    .ToArray();

我先将插座分成阵列,然后过滤对应语。 这种解决办法可能比大管更清洁,因为你可以发现“密码”和过滤器的要求更好。

大型舱面解决办法如边界字——而不是闪.——许多无烟的空间——而不是闪.——字边界。

我将使用一个顶级取代,以取代所有/[a-zA-Z]/然后(无)所有字。

www.un.org/spanish/ecosoc Try this one : (

(s(?<!/)([A-Za-z]+)(?!/))|((?<!/)([A-Za-z]+)(?!/)s)

Using this example excerpt:

The /character/ "_" (underscore/under-strike) can be /used/ in /variable/ names /in/ many /programming/ /languages/, while the /character/ "/" (slash/stroke/solidus) is typically not allowed.

......这种表述与在和之后没有<条码>的“英文”“词”的典型概念”相匹配。

([w ]+)(?<=(?<!/)1|1(?!/))

......也是最纯形式,只使用一个特性类别来界定“词”性质。 例子如下:

Matched               Not Matched
-------------         -------------
The                   character
_                     used
underscore            variable
under                 in
strike                programming
can                   languages
be                    character
in                    stroke
names
many
while
the
slash
solidus
is
typically
not
allowed

如果排除<代码>/stroke/,则不希望添加“极限限制”的比值,则视你如何界定“最大”一词的开头:

([w ]+)(?<=(?<!/)1|1(?!/([^w]))).

。 这将将stroke从“Notequied”移至上文“Matled”清单。

note:w对上或下级信函、编号和强调性<>

如果您想从上述“序号”中改变您的“词”概念,就简单地交换了载于<代码>[w]的部分表达方式,如<代码>[a-zA-Z],以排除数字或<代码>[w -],以包括hy子,其中将捕获作为单一比照,而不是两个单独的对应体:

([w -]+)(?<=(?<!/)1|1(?!/([^w])))

<IMPORTANT ALTERNATIVE!

我只是想到的是Matching <>/em> >上任何“re not则填上/的符号:简单明了consume。 <<>are>/em>上的所有符号和词(分录)。 这有几个好处:没有四舍五入就意味着可以在更多的情况下使用(Javagust不支持眼光,有些小lav不支持统统统统统统统统统统统统),同时提高效率;此外,使用一种分裂的表达方式意味着一种强力阵列的直接结果:

string input = "The /character/ "_" (underscore/under-strike) can be..."; //etc...
string[] resultsArray = Regex.Split(input, @"([^w -]+?(/[w]+/)?)+");

voila!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...